WEBVTT

00:00.000 --> 00:03.900
So I'm here today to talk to you about machine check exceptions.

00:04.220 --> 00:09.980
Basically what happens on your computer when something goes terribly, catastrophically wrong with the hardware.

00:10.340 --> 00:11.820
So why do we want to look at that?

00:11.820 --> 00:16.020
Well, it turns out that it's got some interesting applications for security that nobody's ever looked at before.

00:16.020 --> 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.

00:22.660 --> 00:24.880
So my setup here is on the left.

00:24.880 --> 00:27.020
I've got an x86 platform.

00:27.020 --> 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.

00:31.800 --> 00:35.980
And on the right, we've got a little program running on this machine.

00:36.300 --> 00:42.040
It's just shuffling some letters around just to show us that something's actually running.

00:42.040 --> 00:46.240
And I think we're all probably familiar with what happens on a computer when something goes kind of wrong.

00:46.240 --> 00:50.600
We've probably seen, like, program crashes and seg faults and maybe a blue screen before.

00:50.600 --> 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.

00:57.480 --> 01:03.940
The problem is computers today are actually pretty resilient and things just don't go catastrophically wrong very often.

01:03.940 --> 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.

01:08.820 --> 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.

01:16.740 --> 01:21.300
So I thought, you know, maybe what if we rip off the heatsink from the CPU while the thing's running?

01:21.300 --> 01:24.420
Maybe that'll make something go wrong with the system.

01:24.600 --> 01:29.100
But if we try that, nothing really happened.

01:29.100 --> 01:31.340
We didn't get so much as an error message.

01:31.580 --> 01:37.120
Like I said, computers today are actually pretty resilient and things just don't go terribly wrong all that often.

01:37.120 --> 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 .

01:46.480 --> 01:51.160
So if we really want something to go catastrophically wrong, we're going to have to give it a bigger nudge.

01:51.160 --> 01:55.420
We're going to have to make it so that the CPU can't just run away from its problems.

01:55.580 --> 02:00.280
We'll see if we can overheat this thing enough that underclocking is not going to help it.

02:00.280 --> 02:03.540
So we got to be a little careful here not to melt the rest of the platform.

02:03.540 --> 02:08.840
But if we're precise, we can hopefully make something go wrong with the hardware.

02:08.840 --> 02:09.800
And there it goes.

02:09.800 --> 02:11.400
The whole thing shut down.

02:11.500 --> 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.

02:20.680 --> 02:24.820
And if the CPU cools down a little bit, this thing will boot back up, no worse for wear.

02:24.880 --> 02:26.440
But what's the point of all this?

02:26.440 --> 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.

02:34.840 --> 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.

02:40.480 --> 02:44.840
So the idea here is you've got some code running on the CPU.

02:45.340 --> 02:48.700
You've got, you know, maybe you're setting X to 1 and Y to 2.

02:48.740 --> 02:52.860
But then you've also got other devices connected to the CPU, like a PCIe device.

02:52.860 --> 02:55.560
And that PCIe device might want the CPU's attention.

02:55.560 --> 02:59.500
So it sends like a signal over to the CPU requesting its attention.

02:59.500 --> 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.

03:05.080 --> 03:10.560
It's going to give control over to an interrupt handler instead and then that interrupt handler can service the interrupt.

03:10.560 --> 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.

03:18.480 --> 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.

03:25.320 --> 03:27.180
At least that's how things normally go.

03:27.180 --> 03:28.560
But it's not always that easy.

03:28.560 --> 03:33.080
So suppose you were writing like a Linux kernel module and you wanted to be able to talk to hardware.

03:33.080 --> 03:34.280
Maybe your code looks like this.

03:34.280 --> 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.

03:39.480 --> 03:44.920
But meanwhile, right after you write out that value 200, some other device on the platform interrupts the CPU.

03:44.920 --> 03:48.140
The CPU interrupts your program and gives control to an interrupt handler.

03:48.140 --> 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.

03:54.840 --> 03:56.000
So interrupt handler's happy.

03:56.000 --> 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.

04:02.340 --> 04:06.340
You get the response to the value 100 and your whole program comes falling apart.

04:06.380 --> 04:11.220
So it turns out that some things on a CPU just really shouldn't be interrupted.

04:11.220 --> 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.

04:17.720 --> 04:20.560
So the solution to this is interrupt suppression.

04:20.560 --> 04:25.540
Basically, keep interrupts pending until you finish the work you need to do and then you can service the interrupts.

04:25.540 --> 04:28.720
So that would look something like this if you were writing a Linux kernel module.

04:28.720 --> 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.

04:37.740 --> 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.

04:50.300 --> 04:52.640
It turns out that that means difficulties arise.

04:52.740 --> 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.

05:05.400 --> 05:08.060
Peterson showed something similar in 2018.

05:08.060 --> 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.

05:18.520 --> 05:21.600
Google Project Zero showed something similar in TDX.

05:21.600 --> 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.

05:28.240 --> 05:33.080
Schluter showed using unexpected interrupts to leak information from confidential VMs.

05:33.080 --> 05:38.620
So what we've seen over and over and over is interrupts act as kind of a state disruptor.

05:38.620 --> 05:42.600
They mess up the normal flow of execution in a program.

05:42.600 --> 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.

05:52.860 --> 05:54.840
There's ways to use software to help you with that.

05:54.920 --> 05:56.420
Microcode can help suppress interrupts.

05:56.420 --> 05:58.400
Hardware can help you suppress interrupts.

05:58.480 --> 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.

06:16.440 --> 06:19.280
But then that raises the question, like, can we break through this?

06:19.280 --> 06:20.760
And what would happen if we did?

06:20.760 --> 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.

06:29.400 --> 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.

06:41.000 --> 06:44.860
Even high-energy particles from the sun can cause machine check exceptions.

06:44.860 --> 06:48.980
What happens is the CPU detects the problem, and it generates this MC exception.

06:48.980 --> 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.

06:54.000 --> 06:55.300
And that looks something like this.

06:55.300 --> 06:56.880
You've got your CPU running.

06:56.960 --> 07:00.240
Something bad happens on that CPU, and the CPU detects a problem.

07:00.240 --> 07:01.520
Maybe it's a bit-flipping SRA.

07:01.520 --> 07:03.320
Maybe it's a thermal limit violated.

07:03.320 --> 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.

07:08.460 --> 07:10.700
Then the CPU will generate a machine check exception.

07:10.700 --> 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.

07:17.160 --> 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.

07:23.700 --> 07:25.980
So we had a hardware failure happen.

07:25.980 --> 07:28.080
The machine check was generated by the CPU.

07:28.080 --> 07:29.640
Now the operating system has control.

07:29.640 --> 07:31.360
So what does the operating system do?

07:31.360 --> 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.

07:38.360 --> 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.

07:45.180 --> 07:47.580
They must be handled immediately.

07:47.580 --> 07:49.100
The system is already unstable.

07:49.100 --> 07:50.880
They will cause a machine panic.

07:50.880 --> 07:53.200
The kernel will always panic on a machine check.

07:53.200 --> 07:54.660
It's a bad idea to continue.

07:54.660 --> 07:55.580
There's no way out.

07:55.580 --> 07:57.560
There's no idea what we were executing.

07:57.580 --> 07:58.880
There's no chance to recover.

07:58.880 --> 07:59.860
You must die.

07:59.860 --> 08:01.280
No need to fumble too much.

08:01.280 --> 08:02.200
Just die.

08:02.200 --> 08:03.760
So I think we get the gist here.

08:03.880 --> 08:07.100
The kernel will determine the source of the error if it can.

08:07.160 --> 08:09.020
It'll print a message if it can.

08:09.020 --> 08:14.000
But the most important thing from the kernel's perspective is shut down the system before things get worse.

08:14.080 --> 08:16.280
So why shut down and not fix the problem?

08:16.300 --> 08:20.460
Well, usually with machine checks, you know that something went wrong, but you don't know quite what.

08:20.520 --> 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.

08:24.640 --> 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.

08:30.000 --> 08:31.560
And there's some caveats to this.

08:31.640 --> 08:35.100
On more recent platforms, there's correctable machine checks, and it gets really complex.

08:35.100 --> 08:36.940
But traditionally, this is the strategy.

08:36.940 --> 08:39.160
Shut down as fast as possible.

08:39.200 --> 08:44.580
So all of this makes machine checks entirely unique amongst all the interrupts and exceptions on a platform.

08:44.580 --> 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.

08:53.280 --> 08:59.180
So the only way to avoid handling machine checks is to disable them in what's called the CR4 register.

08:59.400 --> 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.

09:07.660 --> 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.

09:16.100 --> 09:21.580
So what all this means is that our normal solution to interrupt suppression doesn't work anymore.

09:21.580 --> 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.

09:32.520 --> 09:35.180
So all of this gives us a neat opportunity.

09:35.180 --> 09:39.780
Maybe we can use this to build some sort of hammer to break through these interrupt defenses.

09:39.860 --> 09:41.220
But we've got a challenge.

09:41.220 --> 09:43.660
Machine checks are just exceedingly rare.

09:43.660 --> 09:47.660
They're supposed to be this sporadic and unpredictable hardware failures.

09:47.660 --> 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.

09:55.120 --> 09:59.740
And you'll see hundreds of interrupts and exceptions occurring every single second on the platform.

09:59.780 --> 10:02.800
But that highlighted line, machine checks, that'll stay zero.

10:02.800 --> 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.

10:08.420 --> 10:10.000
Well, that's no good for us.

10:10.000 --> 10:11.380
That's no good for exploitation.

10:11.500 --> 10:13.780
We can't just wait 10 years for a machine check to come in.

10:13.780 --> 10:17.040
So then we have to wonder, can we generate these on demand?

10:17.120 --> 10:19.240
There's some way to simulate machine checks.

10:19.240 --> 10:23.300
There's like a tool out there called MCE Inject, but all that does is call the operating system handler.

10:23.820 --> 10:25.660
But that doesn't interrupt anything.

10:25.660 --> 10:30.100
We need real physical machine checks if we want to use these for security purposes.

10:30.520 --> 10:32.920
So I'm diving into that a little bit more.

10:32.920 --> 10:34.460
We need to know how machine checks work.

10:34.460 --> 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.

10:40.380 --> 10:43.400
And you've got different banks devoted to different sources of machine checks.

10:43.400 --> 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.

10:48.040 --> 10:51.840
And there's just many, many options for these different machine check sources.

10:51.840 --> 10:56.360
And you can start dumping all these different banks to see what types of things can cause machine checks.

10:56.360 --> 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.

11:08.140 --> 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.

11:19.180 --> 11:21.960
And the list just goes on and on and on and on and on.

11:21.960 --> 11:27.620
There are hundreds and hundreds of different sources of these different types of problems on a CPU.

11:27.860 --> 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.

11:32.520 --> 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.

11:40.200 --> 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.

11:45.180 --> 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.

11:52.020 --> 11:54.680
So maybe we could start there, and then we can expand on this later.

11:54.680 --> 11:59.960
And all these details are going to vary across different generations of CPUs, but it gives us a starting point.

12:00.080 --> 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.

12:07.080 --> 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.

12:16.060 --> 12:18.540
That's perfect because that's something we can control.

12:18.540 --> 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.

12:25.600 --> 12:28.340
So we can try that, and nothing really happens.

12:28.340 --> 12:31.340
We probably get a master abort, but we don't get a machine check here.

12:31.340 --> 12:32.600
And that's not too surprising.

12:32.600 --> 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.

12:39.540 --> 12:45.400
But still, the data sheets suggest there's some way for a master abort to cause a machine check exception.

12:45.620 --> 12:49.140
So maybe we can figure this out by diving into the north bridge a little bit more.

12:49.140 --> 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,

13:03.100 --> 13:08.080
but none of these had enough information to decide conclusively how do I get a machine check out of this.

13:08.140 --> 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.

13:19.980 --> 13:23.600
And I thought, you know, maybe it's more than one bit that I need to change.

13:23.600 --> 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.

13:31.620 --> 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.

13:38.960 --> 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,

13:53.500 --> 13:58.440
and if it gets a crash or a hang, we can just power cycle the system and try again.

13:58.440 --> 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.

14:09.060 --> 14:12.140
We'll look at those MCA banks to see if any machine checks got logged.

14:12.200 --> 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.

14:19.640 --> 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.

14:27.880 --> 14:29.480
So that looks something like this.

14:29.480 --> 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,

14:41.620 --> 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.

14:50.720 --> 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.

14:59.040 --> 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.

15:06.600 --> 15:08.400
So this was super exciting.

15:08.400 --> 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.

15:18.640 --> 15:20.140
But we've got a problem.

15:20.220 --> 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.

15:31.760 --> 15:34.320
All that lets us do is interrupt our own code.

15:34.580 --> 15:38.020
Basically, we have a hammer now, but we can only hit ourselves with it.

15:38.020 --> 15:39.860
So we need to improve this a little bit.

15:39.860 --> 15:44.520
We need a way for one core to attack a different core with machine checks.

15:44.520 --> 15:46.420
Basically, we need to add a handle to our hammer.

15:46.620 --> 15:51.500
So we can modify the fuzzer a bit here and start looking for more complex bit configurations.

15:51.780 --> 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.

15:58.980 --> 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.

16:08.900 --> 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.

16:15.000 --> 16:17.940
But we've still got an issue, because the platform's still resetting.

16:18.000 --> 16:21.860
So basically at this point, we just need to be a little bit more careful with our hammer.

16:22.280 --> 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.

16:28.480 --> 16:31.520
So what we need to do is figure out, you know, why does the system go down?

16:31.520 --> 16:33.300
It's not actually because of the machine checks.

16:33.300 --> 16:38.120
It's because the operating system decides that's the safest thing to do when it gets a machine check.

16:38.140 --> 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.

16:44.220 --> 16:45.340
So what that looks like is this.

16:45.340 --> 16:50.880
So normally the operating system sets up this IDT, which is the table that describes how to handle different interrupt events.

16:50.880 --> 16:54.420
And one of those entries tells the CPU how to handle a machine check.

16:54.420 --> 16:59.360
What we want to do is replace that interrupt descriptor table with our own, with a hijack IDT.

16:59.380 --> 17:02.940
And the hijack IDT is going to get control first whenever there's any interrupt event .

17:02.940 --> 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 .

17:11.640 --> 17:15.460
That way we get first dibs on any interrupts or exceptions happening on the platform.

17:15.460 --> 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.

17:25.320 --> 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.

17:29.960 --> 17:30.880
So that's kind of dangerous.

17:30.880 --> 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.

17:35.880 --> 17:37.380
So collectively that looks something like this.

17:37.380 --> 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.

17:52.020 --> 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.

17:57.040 --> 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.

18:05.220 --> 18:06.180
So this is great.

18:06.180 --> 18:08.300
This gives us like a state disruptor tool.

18:08.300 --> 18:14.200
It gives us a way to do on-demand generation of machine check exceptions entirely from software.

18:14.320 --> 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.

18:28.120 --> 18:32.140
But the next question then is, well, what do we want to target with this?

18:32.140 --> 18:36.880
So we basically have a great hammer now, we just need to find a nail.

18:36.940 --> 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.

18:45.940 --> 18:47.740
I think there's a lot of ways around that.

18:47.740 --> 18:51.120
But for now, let's look at something more privileged than the kernel, than ring 0.

18:51.120 --> 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.

18:57.640 --> 19:03.140
But it turns out on modern platforms, it actually gets much, much deeper than just the kernel and ring 0.

19:03.180 --> 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.

19:09.980 --> 19:14.240
What I settled on for exploitation here was system management mode as a really appealing target.

19:14.300 --> 19:17.540
I almost didn't pick system management mode just because it's been done before.

19:17.540 --> 19:23.700
But it turns out SMM has some really interesting properties that make it a great case study for machine check exploitation.

19:24.060 --> 19:28.880
So if you're not familiar with SMM, it's actually a pretty neat mode of execution on x86 processors.

19:28.880 --> 19:30.700
It's been around for like 35 years.

19:30.700 --> 19:35.340
It's invisible to the operating system and hypervisor, it can preempt anything that's running on the system.

19:35.360 --> 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.

19:40.760 --> 19:43.840
It's often called ring minus 2, negative 2.

19:43.920 --> 19:47.000
It's critical to platform security and server reliability.

19:47.120 --> 19:51.820
And one of the neatest aspects is it usually has read and write access to a platform's firmware.

19:51.820 --> 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.

19:57.580 --> 19:59.760
But all of that looks something like this.

19:59.760 --> 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.

20:06.880 --> 20:10.540
So firmware's going to start partitioning that memory space into some different regions.

20:10.540 --> 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.

20:19.420 --> 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.

20:31.180 --> 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.

20:41.340 --> 20:43.820
Microcode's going to open up SM RAM temporarily.

20:43.820 --> 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.

20:50.100 --> 20:53.420
So inside of SM RAM, all this code is executing that you never get to see.

20:53.420 --> 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.

20:59.740 --> 21:04.360
It restores the CPU state, it closes SM RAM, and it jumps back to the original code.

21:04.360 --> 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.

21:10.920 --> 21:14.380
So a demo for system management mode is a little bit anticlimactic.

21:14.380 --> 21:18.980
If we want to understand what is inside of this SM RAM, it might be a little unsettling.

21:18.980 --> 21:22.080
There's this whole region of memory on our computers we never get to see.

21:22.080 --> 21:26.700
We can figure out where that memory is by reading a very specific register from the CPU.

21:26.780 --> 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?

21:34.900 --> 21:36.940
The CPU comes back and says, oh, just Fs.

21:36.940 --> 21:37.980
Don't worry about it.

21:37.980 --> 21:39.240
Nothing to see here.

21:39.240 --> 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.

21:44.540 --> 21:47.860
So it's always nice to try to break into these things.

21:47.860 --> 21:48.820
So how could we do that?

21:48.820 --> 21:51.020
How can we compromise this ring minus two?

21:51.120 --> 21:52.440
There's a couple things to keep in mind here.

21:52.440 --> 21:55.820
One is that SMM code is running inside of SM RAM.

21:55.820 --> 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.

22:04.980 --> 22:08.340
And that payload then could unlock system management RAM for us.

22:08.740 --> 22:11.940
So moving forward with this, there's another key aspect we need to understand.

22:11.940 --> 22:17.100
And that's that different CPU modes have to share resources with differently privileged modes.

22:17.100 --> 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.

22:25.540 --> 22:30.740
Ideally, what we'd like to do is we just reset all the CPU registers anytime we change modes.

22:30.740 --> 22:34.120
That way we don't have to worry about what was in those registers before.

22:34.120 --> 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.

22:39.560 --> 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.

22:47.880 --> 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.

22:53.820 --> 23:00.420
So in investigating this, I started to look into the AMD documentation on how they implement system management mode.

23:00.420 --> 23:04.280
And if you dive deep enough into that documentation, you'll see a chart like the chart on the right.

23:04.280 --> 23:09.820
And it shows you exactly which registers get changed when you enter system management mode and which registers stay the same.

23:09.820 --> 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.

23:14.880 --> 23:18.800
We don't want to have interrupts unexpectedly firing when we're entering secure modes.

23:18.800 --> 23:25.280
But one of the things in this chart caught my attention, and that's this IDTR is marked as unmodified.

23:25.280 --> 23:26.100
So why does that matter?

23:26.100 --> 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.

23:35.800 --> 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.

23:50.960 --> 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.

23:58.540 --> 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.

24:08.740 --> 24:14.620
So like anything at all that goes wrong in your code leads to immediate privilege escalation for somebody else.

24:14.620 --> 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.

24:23.880 --> 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.

24:29.220 --> 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.

24:35.900 --> 24:38.700
And I had to get these slides through all sorts of legal and other reviews.

24:38.700 --> 24:42.760
So we're going to call this a feature instead, and then we'll talk about how to exploit the feature.

24:43.000 --> 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.

24:49.440 --> 24:52.700
We just have to make something, anything go wrong on the CPU.

24:52.700 --> 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.

25:00.260 --> 25:04.380
So we've got a problem though, if we want to use machine checks to exploit this issue.

25:04.460 --> 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.

25:13.820 --> 25:17.760
So to understand what we can do about that, we need to dive into SMM code a little bit more.

25:17.760 --> 25:20.900
So the source of this code is called EDK2.

25:20.900 --> 25:26.260
EDK2 is like an open source implementation or reference implementation for EFI firmware.

25:26.260 --> 25:30.460
It's basically showing everybody, here's one way to implement the EFI specification.

25:30.460 --> 25:35.740
And then vendors can take that EDT, EDK2 code and then adapt it for their platforms as needed.

25:35.740 --> 25:42.120
So this is how EDK2 shows you could implement an SMI handler in system management mode.

25:42.120 --> 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.

25:47.320 --> 25:51.020
EDK2 is going to enable handling of machine check exceptions.

25:51.080 --> 25:54.420
And then over here later on, we see an instruction called LIDT.

25:54.420 --> 25:59.100
That's going to reload a new interrupt table inside of system management mode.

25:59.100 --> 26:02.920
So collectively that gives us a couple of different attack windows inside of SMM.

26:02.920 --> 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.

26:16.880 --> 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.

26:22.800 --> 26:28.540
Your attack window's in between that CR4 reload and the upcoming LIDT instruction.

26:28.540 --> 26:31.200
So our machine check attack might look something like this.

26:31.200 --> 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.

26:43.500 --> 26:45.760
But we've got yet another hurdle to overcome.

26:45.760 --> 26:51.720
The system management mode design basically has all threads entering and exiting system management mode simultaneously.

26:51.720 --> 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.

26:59.820 --> 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.

27:05.780 --> 27:07.120
And that's called thread quiescing.

27:07.120 --> 27:10.880
It basically ensures that all threads are inside of SMM at the same time.

27:10.980 --> 27:16.340
And indirectly, that prevents a non-SMM thread from attacking an SMM thread.

27:16.340 --> 27:20.560
And it's a pretty common pattern in designing these privileged execution modes.

27:20.580 --> 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.

27:29.160 --> 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.

27:35.700 --> 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.

27:47.360 --> 27:49.920
So what if we had an attack that looked like this?

27:49.920 --> 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.

27:55.100 --> 27:58.540
And then on a victim thread, the victim thread is going to trigger a system management interrupt.

27:58.540 --> 28:01.000
On x86, you can do that by writing to port B2.

28:01.000 --> 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.

28:09.120 --> 28:13.220
So our victim thread is going to finish its instruction, then get pulled into system management mode.

28:13.220 --> 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.

28:19.780 --> 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.

28:26.000 --> 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.

28:39.000 --> 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,

28:49.860 --> 28:51.780
so that you can't attack it anymore.

28:51.840 --> 28:53.980
So this is where we're at.

28:53.980 --> 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.

29:04.260 --> 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.

29:11.960 --> 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,

29:29.060 --> 29:30.900
and the whole platform is going to reset.

29:30.900 --> 29:35.600
So the question then is, does this magic instruction actually exist?

29:35.600 --> 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?

29:43.180 --> 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.

29:49.760 --> 29:51.640
So how do we build this fuse?

29:51.640 --> 29:53.760
Well, we've got a lot of challenges to overcome.

29:53.760 --> 29:57.220
One is that we need an instruction that can actually generate a master abort.

29:57.220 --> 30:01.140
And master aborts are being done through MMIO on PCI space.

30:01.140 --> 30:06.180
The architecture requires that accessing MMIO follows a very specific form.

30:06.180 --> 30:10.400
You have to use a specific instruction and specific registers to access MMIO.

30:10.400 --> 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.

30:21.400 --> 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.

30:30.360 --> 30:32.180
It's basically fundamentally broken.

30:32.420 --> 30:36.600
So I started thinking about this when I thought, like, maybe there's no way forward here.

30:36.600 --> 30:41.340
And eventually it occurred to me that, you know, not all MMIO is going to be exactly the same.

30:41.380 --> 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.

30:49.260 --> 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.

30:59.280 --> 31:04.500
So what I found is that most things are taking like 700 cycles, but some things are taking like 4,000.

31:04.500 --> 31:07.640
And that's starting to get a little bit closer than where we were before.

31:07.640 --> 31:09.740
So I started wondering, can we increase this somehow?

31:09.740 --> 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.

31:23.220 --> 31:25.160
But then I realized I was doing something stupid.

31:25.160 --> 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.

31:31.540 --> 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.

31:38.160 --> 31:39.720
It has to be a move instruction.

31:39.880 --> 31:42.200
It has to be either EAX, AX, or AL.

31:42.200 --> 31:44.540
That's a 4-byte, 2-byte, and 1-byte register.

31:44.640 --> 31:47.660
And you have to use some memory addressing form to do that.

31:47.660 --> 31:49.800
And it says nothing else is allowed.

31:49.920 --> 31:54.780
So this is what our instruction would normally look like for accessing MMIO space.

31:55.080 --> 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?

32:02.980 --> 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.

32:08.600 --> 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.

32:16.120 --> 32:19.360
So I start to think, what else can we violate in the CPU specs?

32:19.360 --> 32:20.500
Well, there's another thing it tells us.

32:20.500 --> 32:25.100
It says all such accesses are required not to cross any naturally aligned DWORD boundary.

32:25.100 --> 32:27.340
And if you do, you'll get undefined behavior.

32:27.340 --> 32:28.320
Well, that sounds awesome.

32:28.320 --> 32:30.980
Let's find out what undefined behavior looks like in this case.

32:30.980 --> 32:32.460
So this is what our instruction looked like.

32:32.460 --> 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.

32:40.620 --> 32:43.000
So we've got the pieces that we need here.

32:43.000 --> 32:48.200
All we need is a high access time on an existing PCI device, followed by a non-existing device.

32:48.200 --> 32:51.980
That non-existing device is what's going to give us the master abort that'll generate the MCE.

32:52.160 --> 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,

33:04.160 --> 33:07.040
it doesn't even exist, and it's going to generate that master abort.

33:07.180 --> 33:08.800
So we've got all the pieces now.

33:08.800 --> 33:10.320
We've got this fuse instruction.

33:10.320 --> 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,

33:24.260 --> 33:27.140
then no device is going to claim that request, resulting in a master abort.

33:27.140 --> 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.

33:36.800 --> 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.

33:44.360 --> 33:48.220
But we still need that machine check to arrive in a really precise attack window.

33:48.220 --> 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.

33:55.580 --> 33:56.820
And what we got was this.

33:56.820 --> 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.

34:03.960 --> 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.

34:10.040 --> 34:14.620
We can shift that attack window down in order to get the MCE to arrive at the right time.

34:14.620 --> 34:21.320
So with that, we can actually deliver now these cross -core machine checks to victim threads at very, very precise times.

34:21.320 --> 34:23.600
So we've got all the pieces now.

34:23.620 --> 34:26.440
We've got a fantastic tool available to us.

34:26.440 --> 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.

34:30.340 --> 34:32.440
We built a hammer out of machine checks.

34:32.440 --> 34:34.280
We built an MC Hammer.

34:34.580 --> 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.

34:41.280 --> 34:44.920
But you've got your CPU, and you've got some different devices attached to your CPU.

34:44.920 --> 34:48.260
And then on the right side, you've got your RAM, some MMIO space, and other stuff.

34:48.260 --> 34:50.840
While your CPU is booting, the firmware has control.

34:50.840 --> 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.

34:56.560 --> 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.

35:02.660 --> 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.

35:10.220 --> 35:16.740
Meanwhile, we're going to start a victim thread on thread zero and an attacking thread on thread one.

35:16.900 --> 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.

35:29.420 --> 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.

35:39.640 --> 35:44.500
Inside of that hijack IDT, our machine check pointer points to a function that we want to run.

35:44.500 --> 35:47.860
It's going to be the malicious code that we want to run when a machine check is received.

35:47.880 --> 35:51.320
Now both threads are going to start this piece at exactly the same time.

35:51.320 --> 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.

36:00.040 --> 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.

36:08.960 --> 36:11.520
The CPU decodes that address, sends it to the north bridge.

36:11.520 --> 36:14.920
The north bridge decodes that address and sends it to the PCI bus.

36:14.920 --> 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.

36:23.020 --> 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.

36:28.520 --> 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.

36:35.180 --> 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.

36:44.480 --> 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.

36:51.180 --> 36:56.180
Well, the victim thread's going to finish its out instruction very quickly and it moves over to system management mode.

36:56.340 --> 36:59.540
Microcode unlocks SMRAM for the victim thread.

36:59.540 --> 37:00.840
Only the victim thread can see this.

37:00.840 --> 37:02.880
Everybody else still can't see inside of SMRAM.

37:02.880 --> 37:07.580
The victim thread's going to start executing the handler that was installed by the, uh, by the firmware.

37:07.580 --> 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.

37:14.620 --> 37:16.200
This opens up our attack window.

37:16.200 --> 37:19.640
It enables handling of machine checks inside of system management mode.

37:19.640 --> 37:25.140
So our attack window has begun, but very shortly the victim thread is going to load a new interrupt descriptor table.

37:25.140 --> 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.

37:32.380 --> 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.

37:39.200 --> 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.

37:47.980 --> 37:49.340
Nobody's claiming that.

37:49.340 --> 37:51.140
So it's going to create the master abort.

37:51.140 --> 37:54.560
And PCIe parlance, that's an unsupported request, gets sent to the north bridge.

37:54.560 --> 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.

38:00.980 --> 38:04.260
The CPU gets that and translates that as a machine check exception.

38:04.260 --> 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.

38:11.600 --> 38:14.680
Now thread zero needs to figure out how do I handle a machine check exception?

38:14.680 --> 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.

38:20.520 --> 38:25.100
It follows the 18th entry in the IDT and begins executing the code that that points to.

38:25.100 --> 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.

38:30.960 --> 38:33.060
We have no more control of the attacking thread here.

38:33.060 --> 38:35.220
It's just going to execute its handler like normal.

38:35.220 --> 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.

38:47.000 --> 38:49.880
So a full demo of this looks a little bit something like this.

38:49.880 --> 38:55.480
We fire up the MC hammer tool and it's going to replace all the interrupt descriptor tables on each core.

38:55.480 --> 38:57.660
It's going to load up some MC kernel modules.

38:57.660 --> 39:02.820
It's going to do a bunch of timing on different PCI devices to find that fuse instruction that it needs .

39:02.820 --> 39:06.500
It'll poison the north bridge so that small errors become really big errors.

39:06.500 --> 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.

39:12.980 --> 39:17.120
And if we time them right, we gain code execution inside of system management mode.

39:17.120 --> 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.

39:25.380 --> 39:28.920
And that gives us a way to talk to system management mode from the kernel.

39:28.920 --> 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.

39:38.260 --> 39:40.500
So that's the, uh, the memory that dumped on the bottom here.

39:40.500 --> 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.

39:54.900 --> 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.

40:05.020 --> 40:08.780
So we can see that system management mode on the system has access to the platform flash.

40:08.780 --> 40:14.380
We can see it's got all sorts of USB drivers built into it for reasons I don't really understand.

40:14.380 --> 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.

40:22.060 --> 40:26.340
Um, and we'll see the lock box is a way of storing secrets inside of system management mode.

40:26.340 --> 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.

40:31.340 --> 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.

40:40.440 --> 40:46.480
So it's, it's absolutely astounding how many different capabilities have been tossed into system management mode.

40:46.480 --> 40:51.240
And once you gain code execution, you get access to all of those things that we could never touch before.

40:51.300 --> 40:54.560
So where does that leave us as far as mitigations?

40:54.560 --> 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.

41:05.360 --> 41:10.900
Um, on the other hand, the machine check exploit that I developed, I developed this on pre-zen CPUs.

41:10.900 --> 41:13.760
So at least one of these things we can fix ourselves.

41:13.760 --> 41:19.240
So to mitigate the machine check path, um, we can change the way EDK2 works.

41:19.280 --> 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.

41:24.860 --> 41:31.980
So even though that EDK2 SMM code is correct, it does assume that the IDT was being made safe by microcode.

41:31.980 --> 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.

41:42.740 --> 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.

41:48.640 --> 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.

42:00.840 --> 42:02.620
And I think there's still a lot to explore there.

42:03.160 --> 42:04.900
But back to the machine check piece.

42:04.900 --> 42:09.180
I think this is a really exciting piece moving forward, uh, more so than the SMM piece.

42:09.180 --> 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.

42:16.160 --> 42:20.200
And I think there's a whole bunch of interesting ways that we can take this research.

42:20.200 --> 42:24.780
So one is we could start looking at other, uh, sources of machine check exceptions.

42:24.780 --> 42:27.860
Remember, there are hundreds of different ways to cause these different issues.

42:28.160 --> 42:30.660
I looked at one of them coming from the North Bridge.

42:30.660 --> 42:36.420
Uh, we could also look at ECC injection, different processor or rata are documented to cause machine check exceptions.

42:36.420 --> 42:41.140
We could look at doing, like, row hammer style bit flips in DRAM to cause machine check exceptions.

42:41.320 --> 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.

42:49.940 --> 42:55.200
We could look at other MMIO devices to cause asynchronous MCEs or how to do this with no MMIO devices.

42:55.220 --> 43:01.440
The holy grail here would be finding a way to generate machine checks from user land from, from ring three.

43:01.500 --> 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.

43:06.280 --> 43:11.080
You could start looking at accessing faulty devices from user land to see if you can generate machine checks that way.

43:11.100 --> 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,

43:23.980 --> 43:28.860
all those become viable, again, if you can use machine checks to break through those defenses.

43:28.860 --> 43:34.280
We could start looking at other exploit targets like hypervisors and secure guests and enclaves and secure loaders.

43:34.280 --> 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.

43:46.180 --> 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.

43:53.440 --> 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.

44:03.940 --> 44:06.620
So I wanted to give a couple of, uh, resources here.

44:06.620 --> 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.

44:12.840 --> 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.

44:17.360 --> 44:24.260
And there is a huge slew of fantastic research papers on how to use interrupts in unexpected ways for privilege escalation.

44:24.260 --> 44:27.460
And I only got to talk about a couple of those early on in the presentation.

44:27.620 --> 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.

44:35.620 --> 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.

44:40.860 --> 44:45.180
You can find me on most platforms under XOR, EAX, EAX, EAX.

44:45.180 --> 44:49.040
Um, right now I've shared some bare bones stuff on the MC Hammer GitHub repo.

44:49.040 --> 44:52.700
And I'll start pushing more and more stuff there as this issue is better mitigated.

44:52.700 --> 44:56.820
Um, but definitely check that out because, um, like I said, it's just the tip of the iceberg.

44:56.820 --> 44:58.140
So thanks everyone.

