[00:00.000 --> 00:04.900] It's either as a boot kit or in some cases as a firmware implant as we will see later. What's [00:04.900 --> 00:11.540] good also is that code here is hidden from the OS and the hypervisor. So antivirus, [00:11.540 --> 00:18.040] EDRs, or anti-cheat engines won't see these things, right? Now in terms of privilege level [00:18.040 --> 00:22.580] this is just a simplification but we can think about it like this. We have our applications, [00:22.580 --> 00:27.140] like for instance our browser, running at the very top of ring three. Then we have our OS, [00:27.140 --> 00:32.340] ring zero, and if we have a hypervisor it's down the OS of course and only then we have [00:32.340 --> 00:40.340] SMM. Now how does this work? We can divide the uh the thing in two phases. We have boot time [00:40.340 --> 00:47.620] where the firmware BIOS UFI code will after initializing the the hardware will load the SMM [00:47.620 --> 00:54.140] code into an special area of memory called SM RAM. And then it will hand off the execution [00:54.140 --> 01:00.340] to the OS loader then the OS loads and as you can see SMM is just a run time concept. It will [01:00.340 --> 01:05.420] provide services for the for the platform. For instance power management, security, and the [01:05.420 --> 01:10.400] OS can invoke some services from it as well. So how does that happen? Well there's this [01:10.400 --> 01:17.160] concept of system management interrupt. When an SMI occurs the processor gets interrupted and [01:17.160 --> 01:24.040] then this the the current register set of the processor is saved into the SM RAM into a [01:24.040 --> 01:32.940] safety area. And then whatever needs to happen happens some um good feature for the system and [01:32.940 --> 01:37.880] it then comes back to the OS. In this case we are doing a synchronous SMI like a software SMI [01:37.880 --> 01:47.140] the OS is um invoking a service from SMM. Now we've been working on this area for a bit. Uh [01:47.160 --> 01:52.200] we published some some blogs. We've done some some presentations before. How are you on [01:52.200 --> 01:59.700] Hexagon? We have a couple of CVEs in 2023 and we also released some tools for um checking for [01:59.700 --> 02:06.000] misconfigurations and known issues. Um all of these things that we've done before are mostly [02:06.000 --> 02:11.220] related to either misconfigurations from vendors or software vulnerabilities in components. [02:11.220 --> 02:17.200] Now we're going to present today is an architectural flaw in the AMD processor itself. So [02:17.200 --> 02:21.980] that's very different. And so for that we're going to just see again what is what makes the [02:21.980 --> 02:27.500] security of SMM. So we can think on the CPU when it's an SMM mode we have the memory [02:27.500 --> 02:32.280] controller in the middle and we can just access this area with where the SMM code and data [02:32.280 --> 02:37.960] lives. We can execute instructions from this area. We can write to this area and read. However [02:37.960 --> 02:43.660] when the CPU is in normal mode meaning non-SMM, the memory controller just rejects um if if [02:43.660 --> 02:49.700] you read from this uh from this memory you're just going to return receive Fs. Writes are [02:49.700 --> 02:54.860] discarded and of course execution is disallowed. How does this hap- how is this happening? [02:55.780 --> 03:01.960] Well we have um what's called a TSEC region or top of memory segment region. Essentially the [03:01.960 --> 03:09.340] firmware after loads uh after it loads SMM at boot time, it will hopefully configure these [03:09.340 --> 03:16.020] two registers. And the idea is to overlap um have this region to overlap with all the [03:16.020 --> 03:21.140] contents of SM RAM to protect it. We have uh two registers we have the TSEC base and the [03:21.140 --> 03:26.120] TSEC mask which is the length. The TSEC mask as you can see have also uh additional [03:26.120 --> 03:32.660] fields. And yeah essentially this is the way. The memory controller will see that uh the CPU [03:32.660 --> 03:39.200] is executing in normal mode and will just reject to read the content or write the content of [03:39.200 --> 03:47.040] the SM RAM. Has to be enabled. That's why the T-valid is there. Now the layout of SM RAM [03:47.960 --> 03:54.420] uh basically is up to the vendor to define but most systems out there are going to follow [03:54.940 --> 04:01.760] what the EDK2 has which is TianoCore. This is reference code. And uh essentially it's [04:01.760 --> 04:06.400] going to consist at the very bottom you can see here um SMM core area. This is just [04:06.400 --> 04:11.980] supporting code for the rest of of what's going to be in there. And then for each core we're [04:11.980 --> 04:18.280] going to have an SMM base and at a specific offset 8000 from the SMM base we're going to [04:18.280 --> 04:23.380] have the entry point. Which is where the processor, the core is going to start executing [04:23.380 --> 04:29.140] from when the SMI happens. And then we have a safe state at offset FE00. This is TianoCore [04:29.140 --> 04:34.640] right? It can be laid out differently by the vendor. And then we have the SMI handlers at [04:34.640 --> 04:40.060] the very top which are going to be the functions that are are going to be useful for for us [04:40.060 --> 04:46.100] for the system. Handling power, security or whatever. Um so yeah just a summary of the SM [04:46.100 --> 04:50.360] RAM registers. We have the three that we just mentioned and we also have an SMM block. [04:50.360 --> 04:56.640] That's very important. Hopefully the firmware code is going to set this bit so that the [04:56.640 --> 05:01.280] whatever runs at run time the OS cannot tamper with these registers. We don't want for [05:01.280 --> 05:07.460] example at run time having the OS disabling the TSEC location because this will just um [05:08.020 --> 05:12.740] update or change the code at the SM RAM location and execute with these additional [05:12.740 --> 05:19.800] privileges right? Now this needs to be configured for each core. With that we can see a [05:20.360 --> 05:27.000] first difference between how Intel and AMD manage the access to MSRs. On Intel systems [05:27.000 --> 05:34.440] these MSRs that are related to SMM are only accessible while the CPU is in SMM. In fact [05:34.440 --> 05:41.260] obtaining the SMM base for example on Intel could be considered a leak. On AMD all these [05:41.260 --> 05:47.780] MSRs are accessible from ring zero, no problem. Again if we have the SMM block bit set [05:47.780 --> 05:52.540] then the configuration cannot be changed. Not even SMM can change the configuration [05:52.540 --> 05:56.060] until the next power cycle in which I mean the firmware will take over again and [05:56.060 --> 06:03.160] configure this in the same way. But we can now spot the bug in the documentation. This is [06:03.160 --> 06:10.820] the same TSEC mask register and the fields just a picture from the documentation. We can see [06:10.820 --> 06:16.960] how the SMM block is covering all of the fields except for two over there. These two fields [06:16.960 --> 06:22.260] are going to be what we're going to be talking about in this session. And we have T-CLOSE [06:22.260 --> 06:29.260] and A-CLOSE. We're just going to focus on T-CLOSE. A-CLOSE stands for the same feature but [06:29.260 --> 06:33.560] for a previous region called ASEC which overlaps on the video memory. It's not used anymore [06:33.560 --> 06:40.580] so it doesn't matter. But what happens is when this bit is set, the data accesses that the [06:40.580 --> 06:45.080] core that the core performs when it's running in SMM are going to be directed to MMIO [06:45.080 --> 06:52.740] instead of to SMRAM. And that's cool because as we said before, we can set this bit from [06:52.740 --> 06:59.760] ring zero because it's not blocked by the SMM block, right? It actually in the A-CLOSE [06:59.760 --> 07:05.480] description it also says hey, don't forget to unset this when you're returning from SMM [07:05.480 --> 07:13.500] because otherwise your safety are going to be erroneously read from MMIO. That's bad. The [07:13.500 --> 07:18.140] documentation says as well hey, if you have a valid T-CLOSE region and you're in SMM and [07:18.140 --> 07:23.140] the T-CLOSE is set, then instruction fetches are still going to be directed to DRAM but data [07:23.140 --> 07:29.400] accesses are going to go to MMIO instead. If you're not in SMM then both accesses, both type [07:29.400 --> 07:37.200] of accesses are going to go to MMIO. So we can say X86 behaves in a Harvard, like a Harvard [07:37.200 --> 07:42.060] architecture. It goes to Harvard, right? If we have our core zero for example running in [07:42.060 --> 07:47.760] normal mode, both data and instruction fetches just go to MMIO. If we have our core zero now [07:47.760 --> 07:53.500] in SMM with the T-CLOSE off, this is what happens. No problem. We read and execute from SM [07:53.500 --> 07:58.840] RAM. However when we set T-CLOSE on, now data fetches go like this. And this is a problem [07:58.840 --> 08:06.860] because this portion can be controlled by an attacker. So triggering the condition is super [08:06.860 --> 08:13.640] easy. We just need to set this bit, this T-CLOSE bit, and call into an SMI and the system [08:13.640 --> 08:19.700] will immediately freeze, hang, or you're going to spot the problem right there. This feature [08:19.700 --> 08:27.420] exists because, well AMD wanted to have the ability to reuse the physical address space and [08:27.420 --> 08:34.020] have SMM code access an IO device that were using the same SM RAM physical memory [08:34.020 --> 08:40.460] essentially. Um, we haven't seen any vendor using this feature. Um, but yeah, that's why. [08:40.460 --> 08:46.260] And it was, on what we could track is in 2006 there was this document that first mentioned [08:46.260 --> 08:54.600] the feature. So it means it's been around at least for 18 years. Now, for people familiarized [08:54.600 --> 09:00.740] with um, biosecurity they're probably going to remember this vulnerability presented by [09:00.740 --> 09:07.020] Christopher Domas in 2015 called the memory sinkhole attack. And what happened was that he [09:07.020 --> 09:13.520] was able to remap the APIC which is a specific device um, available and overlap it with the [09:13.520 --> 09:18.920] T-SEC area and it caused the same effect. Uh, data fetches will go to MMIO instead of SM RAM. [09:19.140 --> 09:24.480] Now the difference here of course is that that was only limited to the APIC and it only [09:24.480 --> 09:29.600] affected the 4K region where the APIC was overlapped with. The SYNCLOSE behavior [09:29.600 --> 09:36.480] changed uh, how the entire T-SEC works. Um, any device in theory could be overlapped, right? [09:36.480 --> 09:46.880] Because we just direct any access to the MMIO space. So, we can bring some attack ideas in. Our [09:46.880 --> 09:51.780] easy thing will be, the easiest thing will be to just grab any PCI device and hopefully [09:51.780 --> 09:57.340] we'll have a good register layout on its bar so that we can take control of the execution at [09:57.340 --> 10:01.480] the very beginning of the entry point. As there are multiple integrated devices that we [10:01.480 --> 10:06.680] could use, we could just try remapping one of those and based on what we read, the register [10:06.680 --> 10:14.180] should become visible at the T-SEC location from the OS. However this failed. We can see here [10:14.180 --> 10:23.960] how uh, we have an ethernet controller uh, with its original uh, bar 2 uh, D0714000. We have [10:23.960 --> 10:27.900] some registers right here, they're visible registers. And we're going to try to overlap, when [10:27.900 --> 10:34.360] we try to overlap it with the SMM entry uh, BFED0000, we can see that we're still reading F's. [10:34.360 --> 10:42.060] So, the remapping failed. Registers are not available. Um, we can see that the registers were [10:42.060 --> 10:53.940] moved from their original location. Uh, something happened. When we restore, we can see the [10:54.520 --> 11:00.160] the the priority of the memory probably failed, right? So what that that led us to try to [11:00.160 --> 11:04.860] understand okay, how that does, how how is that working? I have this register called top of [11:04.860 --> 11:14.180] memory or TOM. Um, this thing defines a limit between the usable DRAM before 4 gigabytes on the [11:14.180 --> 11:21.880] physical address space and MMIO space on top. Now, on Intel, this register has a lock bit. Once [11:21.880 --> 11:26.300] it's configured, you cannot tamper with it anymore at run time. But on AMD, it doesn't have [11:26.300 --> 11:31.840] such a lock so we can just, you know, modify this and maybe that's good for us. The idea is [11:31.840 --> 11:37.340] simple, we have this is uh, imagine this is the layout, right? And we'll move the MMIO space [11:37.340 --> 11:41.980] of the TOM that defines the MMIO space to match with the TSEC base. So we have something [11:41.980 --> 11:48.580] like that. Maybe then, trying the same trick that we uh, did before could work. [11:48.580 --> 11:54.040] Unfortunately, we could move the TSEC, no problem, but it didn't change the behavior that we saw [11:54.040 --> 12:02.380] before. In practice, it didn't work. So that led us to dig, dig, dig uh, deeper into uh, into [12:02.380 --> 12:07.140] augmentation and we found there are some actual priorities when the core access um, some [12:07.140 --> 12:11.120] portion of the physical address space. Essentially determines what, where it's going. And we [12:11.120 --> 12:16.440] can see that the TSEC and ASIC mechanisms are listed 4, which means 1, 2 and 3, whatever is [12:16.440 --> 12:21.840] listed there has less, less priority than this. And at the, at number 1, we can see the [12:21.840 --> 12:25.780] TOM is, is listed there. So we have, that's why it never worked, right? It would have never [12:25.780 --> 12:35.460] worked. What's at 5, where we have the MMIO configuration space and the APIC? Uh, that [12:35.460 --> 12:41.520] makes sense. We, the APIC was used before in the Intel attack. The MMIO config space we [12:41.520 --> 12:48.960] cannot use really. Um, and then at 6 we have this Norbridge address space uh, routing but uh, [12:48.960 --> 12:53.200] that probably stands for older architectures. This is not seen in modern, modern stuff. So [12:53.200 --> 13:00.220] we cannot use it. For the APIC, we cannot really use it and we're going to explain why. But [13:00.220 --> 13:07.280] for that, I'm going to give an analysis of, of um, how the SMM entry point code works. When [13:07.280 --> 13:13.720] the SMM, when the SMI occurs, the SMM core uh, that, that the core um, executing at SMM [13:13.720 --> 13:19.660] starts executing in real mode. In 16 bit mode, flat. No protection whatsoever. What it has to [13:19.660 --> 13:24.160] do is quickly go to protected mode 32 bit and then goes to long mode and then passes the [13:24.160 --> 13:28.940] execution to the handlers. This is a very simplified view. Now, that first step going to, [13:28.940 --> 13:32.900] from real mode to protected mode is, is key, right? That's the first thing that needs to [13:32.900 --> 13:41.040] happen. For this, um, on x86, we need, essentially are enabled segmentation. Uh, [13:41.040 --> 13:47.440] segmentation provides um, the ability for the, for the core to see the memory in a segmented [13:47.440 --> 13:51.280] way. That's, that's how it's called. Essentially, we can have different segments for [13:51.280 --> 13:56.480] memory and assigns uh, different permissions or like um, the ability to execute code or, or [13:56.480 --> 14:00.400] just read data and stuff like that. We have this key data structure called global [14:00.400 --> 14:06.500] descriptor table of GD or GDT. And we have a register on the processor that is called [14:06.500 --> 14:12.200] GDTR that's going to point to the base of our GDT structure and it's going to have a limit in [14:12.200 --> 14:16.340] bytes. And so the GDT is just really an array of descriptors. And we can have different [14:16.340 --> 14:19.780] types of descriptors. We can have, we can have code descriptors, data descriptors. Each [14:19.780 --> 14:23.580] one is going to have a base. The first one is never used. That's why it's called, it's [14:23.580 --> 14:28.660] called null descriptor. But then, when we have a jump, you can see we'll have um, a jump. [14:28.660 --> 14:32.940] It's not only having an offset like a target location but it's also going to have like a [14:32.940 --> 14:36.820] segment selector right here. That segment selector picks the descriptor we're going to use [14:36.820 --> 14:42.160] for the, for, for our, our code segment. And then the top, the, the offset is added to the [14:42.160 --> 14:48.500] base that is indicated by the GDT. Forming the final linear address. With that we can do [14:48.500 --> 14:57.800] uh, an analysis of um, the EDK2 SMM entry point. Essentially um, we can see that at the very [14:57.800 --> 15:04.840] beginning we are moving into the register BX, the entry point plus 4D. This is, this is a [15:04.840 --> 15:11.120] decom, like um, extracted from a, from, from a compiled um, SMM code. So you can see at 4D [15:11.120 --> 15:16.080] right there we have a GDT description ready to, to, to be loaded into the GDTR. The next [15:16.080 --> 15:19.440] instruction that matters for us is the load GDT. This is the instruction that is going to [15:19.440 --> 15:25.340] load the GDTR with the content that BX points to. And finally, we have a jump over here [15:25.340 --> 15:31.860] that's gonna perform the actual um, going into protected mode. And it has the selector of [15:31.860 --> 15:39.360] code as well and the offset. Now, with this, what, what will happen if we over, overlap a [15:39.360 --> 15:45.640] device that we control on the entry point? And we control offset 4D. What will happen is [15:45.640 --> 15:50.280] that the load GDT will be loading something that we control. So we can load our fake GDT [15:50.280 --> 15:55.320] right? So that's what we're trying to do here. We're trying to get control over the [15:55.320 --> 16:00.800] offset 4D with any device that we can find so that the, the fake GDT then provides the [16:00.800 --> 16:05.560] descriptors we need and we can take control and jump down there. That's the whole idea of [16:05.560 --> 16:12.600] the attack. And that's why the APIC is a problem. Two things. First, the system becomes [16:12.600 --> 16:17.940] unstable when the APIC is moved. Super unstable. Second, the APIC racers are not useful [16:17.940 --> 16:22.740] for taking control in the way we need. We can see that at 4D we have this reserved [16:22.740 --> 16:27.920] regions with all zeros. Rights are discarded here. We're not able to control here and if we [16:27.920 --> 16:33.040] load a GDTR with all zeros we're essentially saying to the processor, hey, here's an [16:33.040 --> 16:39.600] empty GDT. So this won't work. And at this point we found another device not documented by [16:39.600 --> 16:44.500] the, the specification that also has priority over uh, the TSEC region. That's the [16:44.500 --> 16:52.140] spike controller and Christophe's going to describe it for us. Alright. So the spike [16:52.140 --> 16:55.160] controller is a very nice device for us because it's present in all the systems we've [16:55.160 --> 17:01.020] tested and it's basically used to access the spike flash on um, AMD uh, systems. So for [17:01.020 --> 17:04.740] example for reading, writing, erasing the spike flash, it always goes for the spike [17:04.740 --> 17:09.720] controller. And it has a couple of key features that are really uh, interesting to us. First [17:09.720 --> 17:14.560] of all, we can relocate the bar over the SMM entry point. Secondly, the portions which are [17:14.560 --> 17:19.320] interesting to us at offset 4D, we can control them. And three, it actually takes precedence [17:19.320 --> 17:25.020] over uh, SMRAM when TCLOS is enabled. So with that in mind, what we will do for our attack is [17:25.020 --> 17:31.380] simply take the spike bar, relocate it over the SMM entry point of core zero, and then [17:31.380 --> 17:38.420] afterwards manipulate the values at offset 4D within that bar. Um, the um, registers that [17:38.420 --> 17:42.460] actually correspond to that portion of memory in the bar uh, are the memory range and [17:42.460 --> 17:47.560] ROM protection zero register. Those were used in the past to uh, lock down access to the spike [17:47.560 --> 17:53.100] flash, but mostly these are not configured, they're not fixed, so we can fully control them. [17:53.100 --> 17:57.800] On some systems they are fixed, but actually they are fixed to values which are still useful [17:57.800 --> 18:05.260] for us. So, before I dive into the exploits, uh, I also want to uh, show you a little bit our [18:05.260 --> 18:11.260] debugging stuff because it's a little bit special. Um, so we were facing actually two [18:11.260 --> 18:16.740] challenges uh, when we tried to develop the exploit. First of all, we didn't really have a [18:16.740 --> 18:21.840] way to know whether our payload ran because the system just crashes, reboots, and memory is [18:21.840 --> 18:28.320] just all wiped. Uh, and secondly, we didn't really have any debugging framework for SMM. Um, [18:28.320 --> 18:32.740] and like for example when crashes happen, we want to be able to modify the code however we [18:32.740 --> 18:38.220] see fit. So to resolve the first issue, we actually took a PCI squirrel, which is typically [18:38.220 --> 18:44.060] used with PCI leach for DMA attacks, but the nice thing about it is, actually is that it has [18:44.360 --> 18:51.000] a bar buffer, as we call it, where when we write to it, it actually survives across [18:51.000 --> 18:56.540] reboots. So for instance, a payload can write a value to uh, the bar buffer, kind of like a [18:56.540 --> 19:00.320] checkpoint so we know it ran, and then when the system reboots, it's still going to be there. [19:00.320 --> 19:05.420] Right. And if we want to modify, for example, for the second issue, if we want to modify uh, [19:05.420 --> 19:10.400] code in SMM for some extra debugging capabilities, for that we just um, injected a firmware [19:10.400 --> 19:16.480] implant, uh, that loads a nice SMM module that allows us to do all of that. So the setup [19:16.480 --> 19:20.540] looks like this, we just have the PCI squirrel connected to our target device via an M2 [19:21.020 --> 19:27.200] uh, adapter, and all of this is powered externally by a power supply. So, it's time to get our [19:27.200 --> 19:32.180] hands dirty, so let's take a look at how uh, the exploit looks like. In the first attempt, [19:32.180 --> 19:36.920] uh, we remap the spy bar, just like mentioned before, over the SMM entry point of core zero, [19:36.920 --> 19:43.160] then we tweak uh, these values at offset 4D. We create a fake GDT in the beginning of the [19:43.160 --> 19:47.340] address space, uh, with the, with a nice base address that will allow us when the jump [19:47.340 --> 19:53.520] happens to redirect the execution to our own payload. And then when we uh, execute the SMM [19:53.520 --> 19:58.120] entry point code with TCLOS enabled, what's going to happen is uh, the old GDT is actually [19:58.120 --> 20:03.740] going to load the GDT register uh, with a value that points to our own fake GDT, so that's [20:03.740 --> 20:08.480] great. And afterwards, when the jump happens, a descriptor from the fake GDT is going to be [20:08.480 --> 20:17.740] taken that we control, um, and it's going to be used as a base uh, for the jump. So, and um, [20:17.740 --> 20:23.020] this is going to redirect the execution like this, at least in theory. So, um, an interesting [20:23.020 --> 20:30.900] detail is actually how we pick the base address in the GDT descriptor, um, and for that we have [20:30.900 --> 20:36.320] to understand how it normally executes and how it executes with TCLOS in this case. So, um, [20:36.320 --> 20:42.640] when the for jump happens, um, and the normal execution, what happens is the GDT register is [20:42.640 --> 20:47.260] going to point to a GDT within SMRAM where it's nicely protected, um, and the segment [20:47.260 --> 20:52.020] selector is going to index into that table, um, and the base is going to be actually fixed to [20:52.020 --> 20:57.920] zero. Which means with the base of zero when we add it uh, to the for jump offset, what's [20:57.920 --> 21:02.720] going to happen is the linear address is going to be the same. Now in our case, actually we [21:02.720 --> 21:09.620] have a bit of an issue. We want to read the execution, um, outside of TSEG. The only [21:09.620 --> 21:14.820] problem is that within the first four gigs of memory, after TSEG, there's not really any spot [21:14.820 --> 21:19.360] where we can put a payload, right? So, if we increase the base a little bit, sure the [21:19.360 --> 21:23.900] instruction pointer is going to go a bit more up, up, up, up, and the address space, we don't [21:23.900 --> 21:29.120] really have DRAM over there that we control. So we wondered, okay, what happens if we [21:29.120 --> 21:36.460] actually increase the base such that when we, uh, the base of the GDT descriptor, such that [21:36.460 --> 21:44.280] when we add it to the for jump offset, it actually overflows. Um, yeah, it just overflows. [21:44.280 --> 21:48.520] So we thought, okay, this isn't going to be possible, but actually it was. Uh, this is nice [21:48.520 --> 21:53.480] because, uh, if we choose the base sufficiently big and we add the offset, we can actually make [21:53.480 --> 21:56.640] it wrap around and make it jump to our own payload in the beginning of the physical address [21:56.640 --> 22:01.060] space. The beginning of the physical address space, we have DRAM there, great, we control all [22:01.060 --> 22:07.040] of that. So, uh, that was one of the key takeaways actually that we had from the research as [22:07.040 --> 22:14.020] well. So this worked, our payload actually ran, and we saw this because it would write a [22:14.020 --> 22:17.740] value, this nice checkpoint to our bar buffer, but the system would crash and we wondered, [22:17.740 --> 22:21.900] okay, why is this happening? Um, the reason was the SMM safe state. So like Enrique [22:21.900 --> 22:26.840] mentioned, when a CPU goes to SMM mode, it actually saves the current register state into [22:26.840 --> 22:32.040] the, uh, SMM safe state. When it returns, it just restores it. What happens is when TCLOS [22:32.040 --> 22:37.960] is enabled, actually the writes, when the core enters SMM, are going to be discarded, which [22:37.960 --> 22:42.800] means when we return with TCLOS disabled, actually it's just going to use stale values that [22:42.800 --> 22:48.260] were still there, that were stored before. And to resolve this, the trick is actually pretty [22:48.260 --> 22:54.300] straightforward. We trigger the SMM twice. One time, um, without TCLOS and the second time [22:54.300 --> 23:01.340] with TCLOS. So we first put the core zero into a known controlled state, and then trigger an [23:01.340 --> 23:06.400] SMI without TCLOS. This is going to prime the SMM safe state, and afterwards when we trigger [23:06.400 --> 23:11.740] the bug, with TCLOS enabled, the code is going to go for the same execution path, and it's [23:11.740 --> 23:18.560] just going to reuse the values that we stored before. So that worked, but unfortunately it [23:18.560 --> 23:23.200] crashed again. But this time around we did see actually that the, uh, the core zero was [23:23.200 --> 23:27.180] returning from SMM, so that was great. We had full control of core zero, OS code was [23:27.180 --> 23:32.660] running, we saw our checkpoints, great. But no matter what we tried, this damn thing was [23:32.660 --> 23:37.300] still crashing. Um, so we spent an eternity debugging this thing until we stumbled into [23:37.300 --> 23:44.820] this. When we enable TCLOS, what happens is, we enable it on core zero only, but actually it [23:44.820 --> 23:50.540] gets enabled on both cores. Core zero and core one. Um, and we found that the culprit for [23:50.540 --> 23:54.840] this is symmetric multi-threading, so this is AMD's implementation of Intel hyper-threading. [23:55.020 --> 23:59.740] Um, and what it does is it divides a physical core into two logical cores. And some of the [23:59.740 --> 24:04.160] resources on these logical cores are shared and some of them are not. So for instance the [24:04.160 --> 24:11.560] SMM base MSR is separate, so every logical core has a different base MSR. But the TSEC mask is [24:11.560 --> 24:17.780] actually shared, which is what we just saw, right? And we wondered, is this the actual issue? [24:17.780 --> 24:23.100] Because we assume when an SMI gets triggered, that only one core goes into SMM at the time. [24:24.040 --> 24:28.400] And we put this to the test and what we saw is the following. When we have multiple cores [24:28.400 --> 24:34.880] running in normal mode, so outside of SMM, um, when we, when one of them triggers an SMI, an [24:34.880 --> 24:39.480] interrupt gets asserted on all of the cores and all of the cores are going to go into SMM mode [24:39.480 --> 24:47.000] and going to start executing the SMM entry point codes, um, for each and one of them. So, [24:47.000 --> 24:50.940] our assumptions were actually wrong, right? We assumed the SMIs are local, but they're [24:50.940 --> 24:55.620] actually not. And we thought that this was actually the case because the IDK2 code has a [24:55.620 --> 25:00.940] so-called rendezvous routine, which will, every time you enter into SMM, this routine is [25:00.940 --> 25:06.500] going to be triggered and that core which entered into SMM will send an explicit interprocessor [25:06.500 --> 25:12.100] interrupt to all of the logical cores to explicitly pull them from whatever state they were in [25:12.100 --> 25:16.880] into SMM. And, well, what's the point of that if actually all the cores always go into SMM [25:16.880 --> 25:21.380] anyway, right? Um, and we dug a little bit deeper into the documentation and we found that [25:21.380 --> 25:26.300] the IO hub seems to be responsible for this to, uh, basically assert this interrupt on all of [25:26.300 --> 25:34.160] the cores. So, we're out of luck. So, to summarize, we had all cores always going into SMM [25:35.500 --> 25:39.120] and T-Cores being enabled on two cores at the same time and not just one as we initially [25:39.120 --> 25:45.380] thought. And this will actually make just core 1 go haywire, right? We have control of core 0 [25:45.380 --> 25:50.360] but core 1 is, uh, just going to go nuts and crash the whole system. So, to tackle the [25:50.360 --> 25:57.140] problem, we had to get control of core 1 some way. Uh, and we dug deep into our bag of tricks [25:57.140 --> 26:03.140] to really, uh, see if maybe there's a nice elegant solution to this to just take core 1 out [26:03.140 --> 26:10.480] of the running. Um, so we tried to, um, we tried to find another device to overlap with the [26:10.480 --> 26:15.480] entry point of core 1. There was none. Uh, we tried to disable SMT in the BIOS. That also [26:15.480 --> 26:21.940] didn't work. We tried to, uh, use the, uh, so-called init IPI and also the SKL instruction to [26:21.940 --> 26:28.200] put core 1 into a state that would actually make it ignore SMIs. That also didn't work. And [26:28.200 --> 26:32.480] finally, we also tried to send a so-called SMI IPI which, at least according to the [26:32.480 --> 26:38.100] documentation, would allow us to trigger an SMI on individual cores. And it also didn't work. [26:38.100 --> 26:42.880] So, despite the documentation indicating one thing, in practice, we didn't manage to, uh, get [26:42.880 --> 26:47.380] it to work. So, we're running out of options and we had to take a step back and analyze the [26:47.380 --> 26:51.960] problem. And we realized, okay, the LGDT has the actual problem because it's going to be [26:51.960 --> 26:59.360] load- loading the GDT register with all Fs, right? So, is this, is there maybe some way we can [26:59.360 --> 27:05.780] actually still take advantage of this? Um, so, when the supplementary point normally executes [27:05.780 --> 27:09.980] what happens with the GDT register is, it's going to be loaded with a nice value for the base. [27:09.980 --> 27:15.860] It's going to be pointing to the GDT within, uh, SMRAM, right? And afterwards, the segment [27:15.860 --> 27:22.220] selector is going to be taken, added to it, uh, and the base within that, uh, descriptor is [27:22.220 --> 27:28.400] going to be used to calculate the final linear address. Now, in our case, what's going to [27:28.400 --> 27:33.180] happen when TICOS is enabled, uh, the GDT register is going to be loaded with all Fs, both the [27:33.180 --> 27:39.920] limits and the base. And what's going to happen then is, when the for jump, the segment [27:39.920 --> 27:44.280] selector is going to be added to it. And once again, we have our good old friend's integer [27:44.280 --> 27:49.900] overflow. It's actually going to result in the physical address 7. And this is actually [27:49.900 --> 27:54.260] awesome because address 7 in the physical address space, once again, that's DRAM. We control [27:54.260 --> 27:59.480] DRAM. So, we can take advantage of that. We can put our GDT at the beginning of DRAM and we [27:59.480 --> 28:05.880] can just overflow this thing like this. And afterwards, uh, from the GDT we control, we set a [28:05.880 --> 28:12.020] nice base so that afterwards, uh, the code jumps wherever we want it to. So, to summarize, we [28:12.020 --> 28:16.620] have two types of wraparounds. One is between the GDT descriptor base and the for jump offset. [28:16.620 --> 28:22.760] And the second one between the GDT register base and the for jump segment selector. And the [28:22.760 --> 28:28.580] nice thing about this is, we can actually reuse the same fake GDT for both core zero and [28:28.580 --> 28:35.740] core one. And the added bonus is, we don't even need the spy bar anymore. The only thing [28:35.740 --> 28:40.180] that we really have to take care of is the SMM safe state. Once again, for core one. For core [28:40.180 --> 28:45.300] zero, we just do the same thing as before. But core one is going to be running in some [28:45.300 --> 28:49.600] unknown state. It could be running in ring three, ring zero, just in an unknown context. We [28:49.600 --> 28:53.980] have to bring it to the same controlled state as core zero. And we do so by using a kernel [28:53.980 --> 28:59.220] synchronization API. Which on Windows is the deferred procedure call. Or on Linux, [28:59.220 --> 29:05.360] symmetric multiprocessing. And basically once that's done, we have both cores in a known [29:05.360 --> 29:12.400] state. And we can use the same trick we did for core zero. So, all good things are three. So, [29:12.400 --> 29:17.960] let's try again. The exploit this time is greatly simplified. And the only thing we have to [29:17.960 --> 29:23.600] do is, we create the fake GDT in the beginning of the address space. We choose the base [29:23.600 --> 29:29.060] address within the descriptor accordingly. And that's it. And then when we trigger, when we [29:29.060 --> 29:34.540] enable T-Close and run the uh, SMM entry point code again, the old GDT this time around is [29:34.540 --> 29:39.560] going to point to the very end of the address space of the first four gigs. But then when the [29:39.560 --> 29:44.020] far jump happens, it's going to wrap around. It's going to fetch the GDT descriptor entry from [29:44.020 --> 29:49.260] the beginning of the address space from our fake GDT. And then afterwards it's going to [29:49.260 --> 29:55.580] redirect execution for both core zero and core one to our own payload. And it finally [29:55.580 --> 30:00.180] worked. We managed to control both cores, core zero and core one. Uh, our payload would [30:00.180 --> 30:05.660] execute with SMM privileges and uh, it would return gracefully from SMM to the operating [30:05.660 --> 30:09.560] system and nothing would crash. And that was great. There were only a couple of things that [30:09.560 --> 30:16.460] we wanted to improve uh, in our exploit. And that was um, to reload the GDT because the base [30:16.460 --> 30:22.880] actually is um, a bit off. So we just reloaded the GDT so that the base address is equal zero [30:22.880 --> 30:28.060] again so that we don't have strange misalignment issues. We set up long modes, so page [30:28.060 --> 30:34.000] tables, stack pointer, etc. so we can execute like uh, any SQL that we want. Uh, and then we [30:34.000 --> 30:38.600] also install an SMI hander so that we only have to exploit the bug once and then basically we [30:38.600 --> 30:45.640] have kind of semi persistence until the system resets. Right? So, for the demo, just to [30:45.640 --> 30:51.700] show you I'm not selling just some snake oil. Uh, we have a system over here which is a [30:51.700 --> 31:00.000] Huawei Matebook D16 from uh, 2023. And you can see when the system boots, it actually shows [31:00.000 --> 31:06.500] the Huawei logo. That logo is embedded in the firmware code. For that, um, so for our [31:06.500 --> 31:11.860] exploit we're actually going to modify that uh, that logo. So, um, we're going to run our [31:11.860 --> 31:16.360] exploit with uh, kernel privileges. So you'll see over here that we have to elevate [31:16.360 --> 31:22.260] privileges and it's going to be triggered via a malicious driver. And then our exploit is [31:22.260 --> 31:26.500] going to do uh, three things. At first it's going to use the synclose issue to elevate [31:26.500 --> 31:32.220] privileges to SMM. Then it's going to disable the spy flash protections. And then [31:32.220 --> 31:37.660] afterwards it's going to write to the spy flash the portion where the um, boot splash logo [31:37.660 --> 31:42.640] is actually residing. This takes a little while, about 15 minutes, so we just fast forward [31:42.640 --> 31:49.200] that portion. And you can see okay, it shows done patching, all good. And at this point we [31:49.200 --> 32:02.480] just reset the system and pray that it works. So let's see. Yeah, this one takes a little [32:02.480 --> 32:19.880] while. And... there we go. We're successful. So, I'm actually going to show you how to [32:19.900 --> 32:22.580] do this. So I mean this was just one example of what we can do with this kind of access into [32:22.580 --> 32:27.340] the system. But let's take a more systematic approach to see whether this works on all [32:27.340 --> 32:32.760] systems, just on some. What does this actually depend on? So the next attack paths depend on [32:32.760 --> 32:38.600] how the platform is actually configured. Um, so every vendor configures certain features [32:38.600 --> 32:44.460] that AMD provides to them. And specifically there are two of them which are key to this. One [32:44.460 --> 32:49.820] is room armor, which can be used to restrict access to the portions of the spy flash. And [32:49.820 --> 32:54.580] the second one is platform secure boot, which is AMD's equivalent of boot guard, which will [32:54.580 --> 33:00.760] verify the initial stages of the firmware code. Um, so if everything is enabled, the very [33:00.760 --> 33:05.180] least thing that we can do is we can write to a portion of the spy flash where you have your [33:05.180 --> 33:09.280] variables live. So for instance, in these variables you're going to have things like the [33:09.280 --> 33:15.720] secure boot keys which are used. So we can break secure boot. That's nice. Um, but if nothing [33:15.720 --> 33:19.280] is enabled, actually we have firmware implants. I'm going to explain in a moment what the [33:19.280 --> 33:23.480] difference actually is. So in the first configuration where everything is properly [33:23.480 --> 33:28.700] configured, we see, okay, we escalate from the operating system to SMM. And from there we can [33:28.700 --> 33:34.480] write the, let's say, firmware data portion of the spy flash where the configurations are. So [33:34.480 --> 33:38.900] we can manipulate the secure boot keys so that afterwards we can actually load our own [33:38.900 --> 33:44.460] malicious boot loader before the OS loader runs. On the second configuration where the PSB [33:44.460 --> 33:49.920] is not enabled, we have basically the same scenario. Um, Roam Armor will still restrict [33:49.920 --> 33:58.740] write access to, um, to the data portion. And we can, we can still just, um, uh, inject our own [33:58.740 --> 34:04.100] boot kits. And the third configuration we can write to even firmware code, but that's going to [34:04.100 --> 34:09.840] be still verified by platform secure boot. So, okay, and these three, we have the same [34:09.840 --> 34:13.220] scenario. We can just inject a boot kit, which is still nice because it runs before the OS [34:13.220 --> 34:18.520] loader. Any protections provided by the operating system would still be, um, rendered null. [34:19.880 --> 34:24.440] But, in the last scenario where none of these features are enabled, what we can do is we can [34:24.440 --> 34:29.440] inject our, uh, malware into the spy flash, into the firmware code portion of it. And when [34:29.440 --> 34:35.900] that happens, uh, is what we're going to get is we can, um, run our own malware at the very [34:35.900 --> 34:40.880] earliest stage of the boot process. And there's a key difference here between a firmware [34:40.880 --> 34:47.900] implant and, uh, a boot kit, is that this attack actually is resistant to, uh, to OS [34:47.900 --> 34:53.240] reinstallations. So even if you wipe your drive, it's still going to be around. Um, and even if [34:53.240 --> 34:56.840] you try to push a firmware update from the operating system, it's still going to be around [34:56.840 --> 35:02.540] because the, uh, the malware can just say, well, the update was successful. Um, and the only [35:02.540 --> 35:07.940] way to really get rid of it at that point is to take apart your system, take a spy programmer [35:08.360 --> 35:16.400] to read out the spy flash, inspect it, and if it's infected, um, just, uh, rewrite it. But [35:17.340 --> 35:22.980] typically it's not really in the repertoire of a typical IT guy, right? So to illustrate how [35:22.980 --> 35:27.200] prevalent actually these misconfigurations are, we did some research last year on that and [35:27.200 --> 35:30.700] we saw that the majority of the systems have none of the security features actually [35:30.700 --> 35:35.620] enabled. Which means all the systems which have none of the features enabled are [35:35.620 --> 35:40.900] vulnerable to firmware implants. Which is pretty bad. And to put a cherry on top, we also [35:40.900 --> 35:45.460] found that once you're infected with that kind of, uh, firmware implant, you can even disable [35:45.460 --> 35:50.300] platform secure boot forever by burning a specific fuse so that your system remains [35:50.300 --> 35:57.840] vulnerable to firmware, uh, implant reinfections forever. Sweet. So, let's wrap up the [35:57.840 --> 36:07.440] presentation. Thanks Christophe. So, yeah, in terms of affected systems, um, I think [36:07.460 --> 36:13.960] we covered most of them, right? Pretty much all of them. Uh, yeah. Um, our tests were on [36:13.960 --> 36:20.380] Ryzen, mostly. Several laptops. Uh, but turns out the Threat Reaper series is also [36:20.380 --> 36:28.900] affected. Epic series is also affected. Meaning servers. Um, and yeah, uh, due to the [36:28.900 --> 36:37.300] longevity of this, this, this thing, we can estimate the total number of affected chips are in [36:37.300 --> 36:45.320] around the hundreds of millions. Um, there's an advisory from AMD published now, um, where [36:45.320 --> 36:51.560] more information on the system, the affected systems and cover, uh, there's a microcode [36:51.560 --> 36:59.820] update now. Information is there, so you can check. Mitigations. Yeah, AMD, as I just [36:59.820 --> 37:05.580] said, provided, um, with microcode updates. However, it might not cover all of the [37:05.580 --> 37:13.200] affected systems. Of course, um, in terms of what an OEM can do, they can just modify the [37:13.200 --> 37:20.020] SMM entry point code to detect if the T-Close bit is set. And if it is, then just abort the [37:20.020 --> 37:25.620] execution and this, this will just render the exploitation unfeasible. Uh, this can even [37:25.620 --> 37:34.080] be done at the EDK 2 level for AMD systems, right? And we really haven't covered here core [37:34.080 --> 37:39.880] boot, but core boot is affected in the same way. We just, I mean, um, core boot at the very [37:39.880 --> 37:44.640] beginning of the entry point performs a low GDT. They have to go to protected mode as well, [37:44.640 --> 37:50.860] so it's, it's the same thing. Um, and actually core boot, they are checking for this, uh, for [37:50.860 --> 37:57.180] other issues as well at the entry point, so they could, they could very well do this. Um, [37:57.180 --> 38:02.760] and users, well, they can, you know, a hypervisor could trap the access on the TSEC mask [38:02.760 --> 38:10.460] MSR, so that's another, uh, way to stop this. Timeline wise, well, we reported this back in [38:10.460 --> 38:18.480] October of 2023. A CVE of 2023, uh, 31315 was assigned in November. And then we discussed [38:18.480 --> 38:23.500] mitigations and impact with AMD over the course of the next months. Um, and yeah, finally [38:24.100 --> 38:30.340] yesterday, um, the advisory was published and we are here at DEF CON today. This was the [38:30.340 --> 38:38.100] official AMD response. Essentially, we work with them in a process of, uh, responsible [38:38.100 --> 38:45.440] disclosure. And yeah, they've been great to work with. Conclusions and takeaways. Well, the [38:45.440 --> 38:52.360] vulnerability has been around for nearly two decades. Um, the complexity of the architecture [38:52.360 --> 38:57.660] clearly plays in favor of attackers and we see this, uh, with the flexibility of, of, of the [38:57.660 --> 39:04.780] GDT. Um, the two types of wraparounds, the, the, the byte level, uh, um, address, um, [39:04.780 --> 39:11.820] like granularity that there is with it, no problem. Um, and also with the, how the MSRs can [39:11.820 --> 39:17.440] be accessed without any restriction. Uh, that, that's, that's all good for attackers, right? [39:17.440 --> 39:26.320] Um, these things combined are what made this exploit approach possible. It might be on other [39:26.320 --> 39:32.180] exploit methods too. Um, but yeah, all of this of, of course requires an in depth [39:32.180 --> 39:37.080] understanding of the architecture. And in the end we managed to exploit this without [39:37.080 --> 39:42.620] requiring physical presence. So that's very good. And yeah, exploit code will be released [39:42.620 --> 39:47.900] soon. So stay tuned for that. Questions? [39:55.690 --> 40:14.350] Is there a microphone? Hello. Um, it wasn't entirely clear to me, is the spy flash chip [40:14.350 --> 40:21.170] you're referring to on the, uh, CPU die or is it external? External. Okay. Yeah. Um, [40:39.570 --> 40:49.350] so I'm sorry if I understood your question correctly was about bypassing uh, anti-cheat [40:49.350 --> 40:54.710] systems uh, using this method? Nope. Installing something to bypass uh, the anti-cheat [40:54.710 --> 41:03.810] systems and uh, uh. Once you achieve arbitrary code execution in SMM, uh, yeah, pretty much [41:03.810 --> 41:11.110] uh, like modern anti-cheat systems running uh, either kernel or even maybe a hypervisor [41:11.110 --> 41:16.670] level could, could, could run. And this, this runs even below that. Does that make sense? [41:16.670 --> 41:24.270] Yeah, but uh, since uh, MAD has disclosed the processors that are vulnerable to this attack, [41:24.270 --> 41:29.230] it is the, it is not disclosed in PlayStation 5 which has MAD. So that's what I was asking [41:29.230 --> 41:41.330] if maybe it's also, yeah. Ah, okay. But, but yeah, I'm sorry, I, I don't know, uh, I, I don't [41:41.330 --> 41:47.910] understand your question correctly. The, the PlayStation 5 hypervisor blocks the MSR. [41:47.910 --> 41:54.330] If it's for that, yes. The, the, yes. We, we check that. And it is being trapped by the [41:54.330 --> 42:01.230] hypervisor layer on the PlayStation. Yes, sorry. Hey, thank you. Um, quick question. Um, why [42:01.230 --> 42:09.630] didn't disabling SMT prevent the system from becoming unstable? Uh, yes. So the SMT, when we [42:09.630 --> 42:15.850] try to disable it, um, essentially what happens is the other logical core will just not run, [42:15.850 --> 42:20.770] but it'll still be there and it will be, all the entry points will be already configured by [42:20.770 --> 42:24.970] the firmware, if that makes sense. So when the SMI happens, it was a level underneath, I [42:24.970 --> 42:30.550] mean, it won't, you won't see the processor available at the OS level, but all, all the layout [42:30.550 --> 42:36.510] and, and, and, and, at the, at, yeah, at the deeper level, the, the logical core is still [42:36.510 --> 42:40.490] there, if that makes sense. Yes, it does. So we have to deal with that regardless. Yes, we [42:40.490 --> 42:48.610] tried. Thank you. Hello. Do you know what, uh, changes AMD microcode update made? Um, I'm [42:48.610 --> 42:53.950] sorry, could you, could you speak up louder? I'll say. Do you know what changes AMD microcode [42:53.950 --> 42:59.890] update made to mitigate this issue? Oh, interesting question. Uh, yes. Uh, no, I mean, [42:59.890 --> 43:10.270] officially we don't. Uh, um, what, uh, what, what, what, what, what, what, what, what, what, what, [43:10.490 --> 43:24.770] what, what. Uh, we don't. That's a little bit short of it. Uh, yes. I think we're good. Okay. [43:24.770 --> 43:29.470] Well, if no more questions, thank you so much for joining us today.