1 00:00:05,480 --> 00:00:14,880 Good morning, DEFCON. How's everyone doing? Right. So hope you got a good night's sleep 2 00:00:14,880 --> 00:00:21,340 because today we'll embark on a journey. A journey into the realm of Ring Hopper. But 3 00:00:21,340 --> 00:00:27,640 first we'll tell you the story of how we almost zero-dayed the world. So usually a timeline 4 00:00:27,640 --> 00:00:34,700 is presented towards the end of the talk, but we'll make an exception. It all started on June 5 00:00:34,700 --> 00:00:42,980 1st, 2021, when we initially reported the findings we'll be talking about today. On January 19th, 6 00:00:42,980 --> 00:00:50,760 2022, Search CC started working on the coordinated disclosure. We'll soon see why this disclosure 7 00:00:50,760 --> 00:00:56,660 had to be coordinated. By the way, I'd like to use this stage to send a big shout out to VGI and 8 00:00:56,660 --> 00:01:03,520 the folks for mastering a challenging disclosure. Right about now, a year ago, we should have 9 00:01:03,520 --> 00:01:09,180 presented our findings in the Black Hat and DEFCON conferences, but unfortunately we were 10 00:01:09,180 --> 00:01:17,280 notified that the issues will not be fixed in time. So we had to withdraw our talks. But we did 11 00:01:17,280 --> 00:01:25,100 it rather close to the conferences themselves, so our abstract was still printed in the booklet, 12 00:01:26,660 --> 00:01:32,740 which might have caused some confusion. By the way, from the bottom of our hearts, we appreciate 13 00:01:32,740 --> 00:01:42,240 the concern. Fast forward to November 8th, 2022, when the last embargo set by Search CC has 14 00:01:42,240 --> 00:01:51,260 expired. And a note was published, which cleared pretty much everything up. And now that we're out 15 00:01:51,260 --> 00:01:56,120 of the all-day danger zone, where we couldn't talk about our findings, let's dive in. 16 00:01:57,020 --> 00:02:03,820 So at the beginning, there were four privileged rings. The most notable are the user mode, user land, user space. 17 00:02:04,020 --> 00:02:11,720 Ring three goes by many names. And the kernel. The lower the number of the ring, the more privileges it has. 18 00:02:11,980 --> 00:02:19,180 And it works in the manner that is opaque to all the rings above it. For instance, the user mode has no 19 00:02:19,180 --> 00:02:26,640 notion whatsoever of what's going on in the kernel. Over the years, new additional rings were in our 20 00:02:26,660 --> 00:02:31,260 introduced for instance the VMM ring minus one is usually where your 21 00:02:31,260 --> 00:02:38,640 hypervisor runs and SMM ring minus two so let's talk about SMM it started it 22 00:02:39,380 --> 00:02:43,680 stands for system management mode and it started as a small and humble processor 23 00:02:43,680 --> 00:02:49,020 operating mode that's mostly provided low-level system functionalities such as 24 00:02:49,020 --> 00:02:54,480 power management system hardware control and OEMs ran their proprietary device 25 00:02:54,480 --> 00:03:00,560 specific code there over the years the SMM got bigger and bigger and turned it 26 00:03:00,560 --> 00:03:05,640 to huge monstrosity that performed all kind of operations for instance it 27 00:03:05,640 --> 00:03:11,580 exposed and in exposes an interface to the SM BIOS does ps2 mouse keyboard 28 00:03:11,580 --> 00:03:18,560 emulation handles USB events and boot and runtime and the list is long together 29 00:03:18,560 --> 00:03:23,980 with SMM grew the attack surface and it has become so much more interesting for 30 00:03:23,980 --> 00:03:24,400 the attack 31 00:03:24,400 --> 00:03:32,500 but this is fine because numerous registers and MSRs along with some 32 00:03:32,500 --> 00:03:38,640 pretty awesome mitigations were created with the sole purpose to keep SMM safe 33 00:03:39,700 --> 00:03:44,320 so in order to switch to SMM the kernel has to trigger a special kind of 34 00:03:44,320 --> 00:03:50,020 interrupt called system management interrupt or SMI for short once an SMI 35 00:03:50,020 --> 00:03:54,060 is triggered the processor switches to running SMM and the 36 00:03:54,400 --> 00:03:58,840 kernel is no longer active and just like with any other interrupt there is an 37 00:03:58,840 --> 00:04:03,460 interrupt handler that corresponds to the interrupt number that was triggered 38 00:04:03,460 --> 00:04:09,980 and in our case it's aptly named the SMI handler once the SMI handler is 39 00:04:09,980 --> 00:04:14,840 done running the processor switches back to running the kernel and the SMM is no 40 00:04:14,840 --> 00:04:23,000 longer active the SMM resides in a special well-guarded Polish portion of the of the RAM 41 00:04:24,400 --> 00:04:31,320 system management RAM while all the other entities have no access to the SMRAM whatsoever for instance 42 00:04:31,320 --> 00:04:36,400 if the kernel tries to read a bunch um tries to read from the SMRAM it'll get a bunch of effects 43 00:04:37,480 --> 00:04:45,380 uh overall looks like a worthy target target right so in our research we started with an agent 44 00:04:45,380 --> 00:04:51,940 running in user mode escalated through the kernel and from there we helped to the SMM 45 00:04:52,620 --> 00:04:53,860 and we did 46 00:04:53,860 --> 00:05:00,200 all that on this Intel NUC. But as we'll see later on, our research goes way 47 00:05:00,200 --> 00:05:07,680 beyond this recently discontinued little fella. So this is a good time to dive 48 00:05:07,680 --> 00:05:11,680 into our findings, but before we do so we need to understand what is a time of 49 00:05:11,680 --> 00:05:16,240 check time of use vulnerability in SMM context. So let's say we have an SMI 50 00:05:16,240 --> 00:05:21,020 handler that provides the awesome service of overriding a buffer with the 51 00:05:21,020 --> 00:05:26,340 value of coffee. Since we are SMM, we do not trust the operating system, so 52 00:05:26,340 --> 00:05:30,780 the first thing we have to do is to validate the input and basically make 53 00:05:30,780 --> 00:05:34,740 sure that the output buffer points to a valid location. This will be a time of 54 00:05:34,740 --> 00:05:39,960 check. Then, once it was validated we can use it and overwrite its content with 55 00:05:39,960 --> 00:05:44,800 the value of coffee. This will be the time of use. Even attacker can 56 00:05:44,800 --> 00:05:50,160 modify this output buffer to point there to lead to another location in between 57 00:05:50,160 --> 00:05:51,000 the time of check and that's it. 58 00:05:51,000 --> 00:05:55,520 and the time of use, it will basically gain a write primitive of the value coffee. 59 00:05:56,420 --> 00:05:59,100 So, let's see how it works in SMM context. 60 00:05:59,540 --> 00:06:05,220 So, in the normal scenario, we have an input buffer, which would lead to an OS-owned memory, 61 00:06:05,740 --> 00:06:09,880 and the SMI handler will simply override the content of it with the value of coffee. 62 00:06:10,760 --> 00:06:16,480 However, an attacker may try to modify this output buffer to lead to the SMRAM 63 00:06:16,480 --> 00:06:19,080 in between the time of check and time of use. 64 00:06:19,080 --> 00:06:23,760 And by doing so, gain a write primitive of the value coffee to the SMRAM. 65 00:06:24,180 --> 00:06:28,200 By the way, this is exactly what a validation function does. 66 00:06:28,400 --> 00:06:32,920 It makes sure that the output buffer is not located inside the SMRAM. 67 00:06:33,500 --> 00:06:36,040 So, let's discuss how we exploit such an issue. 68 00:06:36,540 --> 00:06:40,560 In a classic time of check, time of use exploitation, it will work as follows. 69 00:06:40,920 --> 00:06:43,140 Let's say we have a system with four different cores, 70 00:06:43,320 --> 00:06:47,240 and let's assume that the first core does the critical SMI handling. 71 00:06:47,440 --> 00:06:48,960 It does the validation and use. 72 00:06:49,080 --> 00:06:52,860 So, within the other cores, we will try to corrupt the data. 73 00:06:53,320 --> 00:06:55,500 Sometimes, we may do this before the validation, 74 00:06:56,100 --> 00:06:58,660 and if so, it will simply not pass the validation phase. 75 00:06:59,140 --> 00:07:03,140 Sometimes, after the usage, and in such scenario, nothing would happen. 76 00:07:03,540 --> 00:07:07,000 But, eventually, with some luck, we may corrupt the data 77 00:07:07,000 --> 00:07:10,820 exactly in between the validation and usage, and exploit this issue. 78 00:07:11,620 --> 00:07:15,740 However, when an SMI is triggered, all the other cores are going idle, 79 00:07:15,920 --> 00:07:18,200 and we cannot use them to corrupt the data. 80 00:07:19,080 --> 00:07:19,960 So, how did we do that? 81 00:07:20,500 --> 00:07:22,040 The answer was DMA. 82 00:07:22,260 --> 00:07:24,540 DMA stands for Direct Memory Access, 83 00:07:24,860 --> 00:07:26,860 and this is the way for your peripheral devices 84 00:07:26,860 --> 00:07:29,020 to talk with the CPU, the operating system. 85 00:07:31,160 --> 00:07:35,240 And, in precise, those devices could be your Ethernet adapter, 86 00:07:35,540 --> 00:07:37,320 or your graphics card, and etc. 87 00:07:38,020 --> 00:07:39,260 So, to test this hypothesis, 88 00:07:39,720 --> 00:07:41,800 we took something that is called PCI-LH. 89 00:07:41,960 --> 00:07:43,820 It's a cool tool by Ulf Rysk. 90 00:07:43,940 --> 00:07:46,960 This is the small red FPGA device that you see here, 91 00:07:47,040 --> 00:07:48,620 which is connected through the M2 bus. 92 00:07:49,920 --> 00:07:53,660 And, basically, it allows you to initiate arbitrary read and write 93 00:07:53,660 --> 00:07:55,920 DMA transactions to the RAM. 94 00:07:56,680 --> 00:07:59,420 And, using it, we were actually able to exploit 95 00:07:59,420 --> 00:08:00,520 this time-of-check, time-of-fuse issue, 96 00:08:00,680 --> 00:08:03,960 which was amazing, but, as some of you may say, 97 00:08:04,160 --> 00:08:05,340 it's not good enough. 98 00:08:05,520 --> 00:08:07,160 It requires physical access, right? 99 00:08:07,540 --> 00:08:12,520 And, it could be considered only as an evil main attack, 100 00:08:12,640 --> 00:08:14,360 or supply chain attack at best. 101 00:08:14,720 --> 00:08:16,200 And, we thought so as well, 102 00:08:16,320 --> 00:08:18,640 and we wanted to make things even more powerful. 103 00:08:19,080 --> 00:08:20,820 We wanted to operate them remotely. 104 00:08:21,240 --> 00:08:24,900 So, to do that, we utilized something that exists on any platform. 105 00:08:25,100 --> 00:08:26,500 We utilized a storage device. 106 00:08:27,320 --> 00:08:30,160 And, we did it using a cool technique by Rafal, 107 00:08:30,300 --> 00:08:32,220 that was presented a couple of years ago, 108 00:08:32,340 --> 00:08:35,640 which allows you to generate DMA transactions using a storage device. 109 00:08:36,640 --> 00:08:38,360 So, let's see how it works now. 110 00:08:38,840 --> 00:08:40,200 Remember the previous diagram? 111 00:08:40,780 --> 00:08:42,860 Now, we add another component into the equation. 112 00:08:43,140 --> 00:08:44,260 We add your storage device. 113 00:08:44,680 --> 00:08:46,540 And, on top of it, we place some malicious file, 114 00:08:46,620 --> 00:08:47,520 which contains our payload. 115 00:08:47,520 --> 00:08:49,500 Or, basically, the modified address. 116 00:08:50,740 --> 00:08:51,940 So, once again, 117 00:08:52,600 --> 00:08:54,880 we will start by generating a DMA request. 118 00:08:55,320 --> 00:08:58,020 Due to the asynchronous nature of the storage device, 119 00:08:58,400 --> 00:09:00,540 it will take it some time to process the request. 120 00:09:01,380 --> 00:09:04,820 Then, we will also hook this request 121 00:09:04,820 --> 00:09:07,780 such that it will be redirected back to the input buffer. 122 00:09:08,320 --> 00:09:10,140 And, we will trigger an SMI. 123 00:09:11,620 --> 00:09:13,260 Once we trigger an SMI, 124 00:09:13,320 --> 00:09:14,700 all the cores are going idle. 125 00:09:15,360 --> 00:09:15,840 Remember? 126 00:09:15,840 --> 00:09:17,660 Then, eventually, 127 00:09:17,880 --> 00:09:21,860 the SMI handler will enter the critical validation and usage steps. 128 00:09:22,600 --> 00:09:23,720 And, with some luck, 129 00:09:23,940 --> 00:09:26,760 the data or the file will be returned 130 00:09:26,760 --> 00:09:29,040 exactly in between the validation and usage 131 00:09:29,040 --> 00:09:32,560 and will allow us to exploit this time-of-check, time-of-use issue. 132 00:09:34,120 --> 00:09:34,560 Okay. 133 00:09:34,720 --> 00:09:35,980 So, we talked a lot. 134 00:09:36,160 --> 00:09:37,860 And, it's probably a good time for a recap. 135 00:09:38,520 --> 00:09:40,660 So, we discussed what is SMM 136 00:09:40,660 --> 00:09:42,460 and how we can communicate with it. 137 00:09:42,680 --> 00:09:45,820 We also discussed how we can turn those time-of-check, time-of-use issues 138 00:09:45,840 --> 00:09:48,120 into write primitives to the SMRAM. 139 00:09:48,700 --> 00:09:51,640 And, we also discussed how to manipulate DMA transactions 140 00:09:51,640 --> 00:09:54,680 to exploit exactly those time-of-check, time-of-use issues. 141 00:09:55,740 --> 00:09:58,600 And, we also discussed how to execute code inside SMM. 142 00:09:58,840 --> 00:10:00,520 Oh, well, not exactly. 143 00:10:02,220 --> 00:10:03,040 So, yeah. 144 00:10:03,120 --> 00:10:06,520 We don't currently have the ability to execute code in SMM, 145 00:10:06,560 --> 00:10:08,240 but we have something pretty powerful. 146 00:10:08,380 --> 00:10:10,280 We have a write primitive to the SMRAM. 147 00:10:10,400 --> 00:10:12,620 So, let's put that to some good use. 148 00:10:13,020 --> 00:10:14,700 One of the modules we analyzed 149 00:10:14,700 --> 00:10:17,380 was the SMBIOS DMA edit Dixie driver. 150 00:10:17,700 --> 00:10:20,100 And, our analysis was not in vain. 151 00:10:20,220 --> 00:10:23,740 It bore fruits in the image of potential write primitives. 152 00:10:25,220 --> 00:10:28,000 It's worth noting that this list is not exhaustive 153 00:10:28,000 --> 00:10:32,340 and it's far from being the only vulnerable module. 154 00:10:32,940 --> 00:10:36,300 However, with a bunch of write primitives in a classic scenario, 155 00:10:36,860 --> 00:10:40,660 you would probably try to find an executable memory region, right? 156 00:10:41,040 --> 00:10:44,680 And, we thought that it would apply to SMM as well. 157 00:10:44,700 --> 00:10:47,440 But, as we discovered, 158 00:10:47,800 --> 00:10:52,100 SMM has a paging mechanism of its own 159 00:10:52,100 --> 00:10:55,300 where all pages containing code are read-only 160 00:10:55,300 --> 00:10:58,600 and all pages containing data are non-executable. 161 00:10:59,280 --> 00:11:01,580 Then, you would probably try to forge 162 00:11:01,580 --> 00:11:04,880 some arbitrary payload out of your write primitives. 163 00:11:05,480 --> 00:11:08,020 And, even though we have quite a few of them, 164 00:11:08,140 --> 00:11:12,220 their variety and number does not let us craft anything decent. 165 00:11:12,760 --> 00:11:13,860 And, lastly, 166 00:11:13,860 --> 00:11:17,300 you would strive to get unrestricted memory access. 167 00:11:17,660 --> 00:11:19,400 And, here, once again, 168 00:11:19,600 --> 00:11:24,300 the paging mechanism in the SMM hinders our exploitation. 169 00:11:24,880 --> 00:11:26,120 So, for starters, 170 00:11:26,400 --> 00:11:31,240 most of the non-SMRAM RAM is simply not mapped to the page table. 171 00:11:31,520 --> 00:11:33,520 And, if that's not annoying enough, 172 00:11:33,700 --> 00:11:34,360 and it's annoying, 173 00:11:34,620 --> 00:11:38,400 the page table resides in read-only pages, 174 00:11:38,540 --> 00:11:40,680 meaning no new pages can be allocated 175 00:11:40,680 --> 00:11:43,100 and all attributes are permanent. 176 00:11:43,760 --> 00:11:47,560 Meaning that if we do manage to execute code in SMM, 177 00:11:47,600 --> 00:11:49,060 it would be pretty hard, 178 00:11:49,120 --> 00:11:49,760 not impossible, 179 00:11:50,120 --> 00:11:51,760 but pretty hard to do something, 180 00:11:51,900 --> 00:11:53,740 for instance, to the OS. 181 00:11:55,620 --> 00:11:58,300 So, as you all probably agree, 182 00:11:58,540 --> 00:11:59,520 I hope by now, 183 00:12:00,180 --> 00:12:03,580 the classic approach might not be very applicable here. 184 00:12:03,820 --> 00:12:06,280 So, let's try to think out of the box 185 00:12:06,280 --> 00:12:07,720 and leverage some mechanisms 186 00:12:07,720 --> 00:12:11,700 that are unique and internal to SMM. 187 00:12:13,340 --> 00:12:14,680 So, at this point, 188 00:12:15,460 --> 00:12:20,520 Jonathan and I turned to reading some works from the past. 189 00:12:20,680 --> 00:12:23,140 And one of the works we found 190 00:12:23,480 --> 00:12:27,460 was by Rafael Waschuk and Corey Kallenberg from 2015, 191 00:12:27,880 --> 00:12:28,700 where they write, 192 00:12:28,940 --> 00:12:31,720 by forcing S3 suspend resume cycle, 193 00:12:31,820 --> 00:12:33,580 an attacker can run an arbitrary code 194 00:12:33,580 --> 00:12:35,500 and take control over SMM. 195 00:12:35,660 --> 00:12:37,140 Sounds promising, right? 196 00:12:37,240 --> 00:12:39,560 But what's S3 suspend resume? 197 00:12:39,720 --> 00:12:40,960 And what is S3? 198 00:12:41,700 --> 00:12:44,000 So, well, S3 is a sleep state 199 00:12:44,000 --> 00:12:45,740 where the CPU is idle, 200 00:12:45,820 --> 00:12:47,520 but the memory keeps on working. 201 00:12:47,920 --> 00:12:49,060 And simply put, 202 00:12:49,280 --> 00:12:50,760 when you put your machine to sleep, 203 00:12:50,920 --> 00:12:52,600 it usually enters S3. 204 00:12:53,040 --> 00:12:54,760 Let's continue with some more background. 205 00:12:56,680 --> 00:12:58,300 So, a normal boot process 206 00:12:58,300 --> 00:13:01,040 is rather time-consuming. 207 00:13:01,380 --> 00:13:03,680 But when we come back from S3, 208 00:13:03,760 --> 00:13:05,500 some of this process can be skipped 209 00:13:05,500 --> 00:13:08,500 because, well, the machine has already booted once. 210 00:13:09,100 --> 00:13:11,420 However, some configurations 211 00:13:11,700 --> 00:13:13,060 still needs to be restored. 212 00:13:13,580 --> 00:13:17,660 And here, the S3 boot script comes to our aid. 213 00:13:18,100 --> 00:13:19,520 During the normal boot, 214 00:13:19,740 --> 00:13:21,300 the configuration of the platform 215 00:13:21,300 --> 00:13:23,740 is encoded into the S3 boot script, 216 00:13:24,000 --> 00:13:27,040 and then when the machine wakes up, 217 00:13:28,300 --> 00:13:30,440 there is the code boot, 218 00:13:30,600 --> 00:13:32,600 and the code boot engine 219 00:13:32,600 --> 00:13:35,660 simply executes the script 220 00:13:35,660 --> 00:13:37,500 and restores the configuration. 221 00:13:37,980 --> 00:13:39,780 And this boot script consists 222 00:13:39,780 --> 00:13:41,680 of rather primitive operations 223 00:13:41,700 --> 00:13:43,120 of codes like reading, writing, 224 00:13:43,220 --> 00:13:46,360 to I.O., to memory, to PCI, etc. 225 00:13:46,740 --> 00:13:48,260 And here's the interesting bit. 226 00:13:49,320 --> 00:13:51,420 Previously, it was stored in a location 227 00:13:51,420 --> 00:13:53,420 that was accessible to the OS, 228 00:13:53,700 --> 00:13:55,900 which is exactly the fact 229 00:13:56,680 --> 00:14:00,060 that the paper we saw earlier abuses. 230 00:14:00,480 --> 00:14:03,420 They modified the boot script from the kernel, 231 00:14:03,680 --> 00:14:06,180 put the machine to sleep, woke it up. 232 00:14:06,340 --> 00:14:09,200 At this point, their malicious S3 boot script 233 00:14:09,200 --> 00:14:10,080 was executed, 234 00:14:10,080 --> 00:14:12,760 and eventually they ran code in SMM. 235 00:14:13,720 --> 00:14:17,060 So, as you could probably guess, 236 00:14:17,420 --> 00:14:20,800 this paper was from 2015, 237 00:14:21,200 --> 00:14:22,540 so it's already mitigated. 238 00:14:22,720 --> 00:14:24,620 And nowadays, the S3 boot script 239 00:14:24,620 --> 00:14:28,520 resides in a container aptly named Logbox. 240 00:14:28,800 --> 00:14:31,820 And the Logbox is a data structure 241 00:14:31,820 --> 00:14:34,360 that provides integrity of the data. 242 00:14:34,520 --> 00:14:36,660 It is located in the SM RAM, 243 00:14:36,820 --> 00:14:40,060 has a pretty standard image 244 00:14:40,080 --> 00:14:40,420 implementation, 245 00:14:40,840 --> 00:14:43,060 and there is a rather convenient API 246 00:14:43,060 --> 00:14:45,440 for reading, writing, and updating it 247 00:14:45,440 --> 00:14:46,680 from ring zero. 248 00:14:47,180 --> 00:14:49,320 So, let's have a look at the update procedure. 249 00:14:50,440 --> 00:14:52,940 So, here we see the function 250 00:14:52,940 --> 00:14:54,800 that updates the Logbox, 251 00:14:55,000 --> 00:14:58,980 and it either returns access denied error 252 00:14:58,980 --> 00:15:00,620 or some internal status. 253 00:15:01,480 --> 00:15:03,980 The only way we do not get access denied, 254 00:15:04,180 --> 00:15:06,040 and, well, we don't want to get access denied, 255 00:15:06,180 --> 00:15:08,900 is if the M locked global variable 256 00:15:08,900 --> 00:15:09,820 is not set, 257 00:15:10,780 --> 00:15:12,920 which is kind of a nice way 258 00:15:12,920 --> 00:15:16,720 of preventing the kernel 259 00:15:16,720 --> 00:15:18,940 from updating the Logbox. 260 00:15:20,160 --> 00:15:22,580 And this global variable 261 00:15:23,440 --> 00:15:25,560 unconveniently resides in the SM RAM, 262 00:15:25,740 --> 00:15:28,320 but, hey, we have write primitive there, right? 263 00:15:28,880 --> 00:15:33,660 So, let's utilize all the information we got 264 00:15:33,660 --> 00:15:37,120 and try to execute unauthorized code in SMM. 265 00:15:37,280 --> 00:15:38,740 So, the plan is as follows. 266 00:15:38,900 --> 00:15:40,360 We use our write primitive 267 00:15:40,360 --> 00:15:43,080 to zero out the M locked global variable. 268 00:15:43,520 --> 00:15:46,520 Then we invoke the update procedure 269 00:15:46,520 --> 00:15:47,580 of the Logbox, 270 00:15:47,740 --> 00:15:50,840 and by that, modify the S3 boot script 271 00:15:50,840 --> 00:15:52,840 with our malicious version of it. 272 00:15:53,580 --> 00:15:55,520 We put the machine to sleep, 273 00:15:55,680 --> 00:15:56,480 wake it up, 274 00:15:57,100 --> 00:15:58,440 and at this point, 275 00:15:58,520 --> 00:16:00,780 the S3 boot script will be executed 276 00:16:00,780 --> 00:16:03,720 and will be eventually running code in SMM. 277 00:16:04,640 --> 00:16:06,440 So, it's a great idea, 278 00:16:06,640 --> 00:16:08,020 but it doesn't, 279 00:16:08,020 --> 00:16:09,540 it doesn't really work, 280 00:16:11,520 --> 00:16:14,100 because right after the machine wakes up 281 00:16:14,100 --> 00:16:17,100 and just before the S3 boot script is executed, 282 00:16:17,400 --> 00:16:19,820 the code will close and lock SM RAM 283 00:16:19,820 --> 00:16:21,940 and only then jump to the boot script, 284 00:16:22,160 --> 00:16:27,400 meaning that even though we managed to execute code, 285 00:16:27,540 --> 00:16:29,800 rather privileged code, 286 00:16:30,000 --> 00:16:33,720 at rather early stages of S3 resume, 287 00:16:34,000 --> 00:16:36,020 the SM RAM is locked 288 00:16:36,020 --> 00:16:37,860 and most of the RAM is simply 289 00:16:38,020 --> 00:16:39,440 not mapped to the page table. 290 00:16:41,080 --> 00:16:43,340 Yeah, we should have read the documentation. 291 00:16:44,100 --> 00:16:45,960 All right, so our quest continues. 292 00:16:46,360 --> 00:16:48,360 And let's take another attempt. 293 00:16:48,540 --> 00:16:52,720 And let's take another attempt 294 00:16:52,720 --> 00:16:54,700 where we emerge victorious. 295 00:16:55,240 --> 00:16:57,900 But as usual, some background. 296 00:16:58,380 --> 00:17:02,100 So, each core has its own region of the SM RAM 297 00:17:02,100 --> 00:17:04,720 pointed to by the SM base MSR, 298 00:17:04,780 --> 00:17:05,880 model-specific register. 299 00:17:06,100 --> 00:17:08,000 Each core has an MSR of its own. 300 00:17:08,020 --> 00:17:11,000 Each of these regions contains 301 00:17:11,000 --> 00:17:12,280 a lot of interesting things, 302 00:17:12,420 --> 00:17:13,620 but we'll be focusing on two. 303 00:17:14,140 --> 00:17:16,440 The SMI handler entry point, 304 00:17:16,580 --> 00:17:18,540 which is the first code that will be executed 305 00:17:18,540 --> 00:17:19,900 upon entering SMM, 306 00:17:19,940 --> 00:17:21,200 and the save state, 307 00:17:21,420 --> 00:17:23,740 which contains, among other things, 308 00:17:24,020 --> 00:17:26,680 the value of the SM base MSR. 309 00:17:27,660 --> 00:17:29,480 And as a matter of fact, 310 00:17:29,640 --> 00:17:32,340 the proper way to modify the SM base MSR 311 00:17:32,340 --> 00:17:35,020 is by changing its value in the save state. 312 00:17:35,560 --> 00:17:37,920 And this whole operation, 313 00:17:37,920 --> 00:17:39,020 this question is pretty special. 314 00:17:39,200 --> 00:17:40,900 How special is it, you might wonder. 315 00:17:41,140 --> 00:17:43,020 It's so special that it has a name. 316 00:17:43,720 --> 00:17:45,180 SM base relocation. 317 00:17:45,420 --> 00:17:47,180 From a developer's point of view, 318 00:17:47,280 --> 00:17:49,540 SM base relocation is pretty straightforward. 319 00:17:50,100 --> 00:17:51,560 They modify the value, 320 00:17:52,300 --> 00:17:54,580 the SM base value in the save state, 321 00:17:54,780 --> 00:17:56,700 and some magic behind the scene 322 00:17:56,700 --> 00:17:57,840 relocates it, 323 00:17:57,980 --> 00:18:00,640 or relocates the whole region to the new, 324 00:18:01,660 --> 00:18:02,840 to the new address. 325 00:18:03,660 --> 00:18:05,960 Which leads to the following attack idea. 326 00:18:05,960 --> 00:18:09,940 So let's allocate some user-controlled memory 327 00:18:09,940 --> 00:18:14,060 that is accessible to both kernel and SMM. 328 00:18:14,160 --> 00:18:18,080 There we'll craft our own SMI handler entry point, 329 00:18:18,160 --> 00:18:21,020 the first code that will be executed upon entering SMM. 330 00:18:21,720 --> 00:18:24,620 And now we'll simply use our write primitive 331 00:18:24,620 --> 00:18:28,000 to modify the SM base value in the save state. 332 00:18:28,220 --> 00:18:31,340 This will trigger an SM base relocation, 333 00:18:31,420 --> 00:18:34,920 and eventually we'll execute code in SMM, right? 334 00:18:35,100 --> 00:18:35,480 Huh? 335 00:18:35,960 --> 00:18:38,120 It's a great idea, but it doesn't work. 336 00:18:38,680 --> 00:18:40,940 It doesn't work at least out of the box, 337 00:18:41,000 --> 00:18:43,300 and it doesn't work thanks to this MSR. 338 00:18:43,360 --> 00:18:47,040 And in particular, the SMM code check enable bit. 339 00:18:48,520 --> 00:18:50,420 So when this bit is set, 340 00:18:50,580 --> 00:18:52,580 and the machine is running in SMM, 341 00:18:54,280 --> 00:18:58,200 if someone tries to execute code outside of SMRAM, 342 00:18:58,260 --> 00:18:59,680 it will simply trigger an exception. 343 00:18:59,980 --> 00:19:02,740 You can think of it as some sort of a SMAP mechanism. 344 00:19:03,400 --> 00:19:04,460 But that's not all. 345 00:19:04,460 --> 00:19:07,580 There is a log bit here that if set, 346 00:19:07,700 --> 00:19:11,880 this MSR cannot be modified or changed in any way 347 00:19:11,880 --> 00:19:13,040 until the next reboot. 348 00:19:13,400 --> 00:19:17,120 And while rebooting the machine or cutting power to the CPU 349 00:19:17,120 --> 00:19:18,820 kind of defeats the purpose. 350 00:19:18,920 --> 00:19:22,440 So at this point, Jonathan and I sat and thought, 351 00:19:22,540 --> 00:19:26,600 what could be the closest thing to rebooting the machine 352 00:19:26,600 --> 00:19:27,940 without rebooting the machine? 353 00:19:28,540 --> 00:19:30,080 And then it hit us. 354 00:19:30,160 --> 00:19:31,820 We'll just put the machine to sleep, 355 00:19:31,920 --> 00:19:34,240 and in particular to S3 sleep. 356 00:19:34,860 --> 00:19:37,120 Recall that the S3 sleep state 357 00:19:37,120 --> 00:19:40,180 is a processor state where the CPU is idle, 358 00:19:40,280 --> 00:19:41,600 but the memory keeps on working. 359 00:19:42,440 --> 00:19:44,720 And as the CPU loses power, 360 00:19:44,980 --> 00:19:47,120 all of the entries stored on the CPU 361 00:19:47,120 --> 00:19:49,520 are cleared when going into S3. 362 00:19:49,660 --> 00:19:51,820 So if you care about all these entries, 363 00:19:52,000 --> 00:19:53,780 for instance, the MSR we just saw, 364 00:19:54,820 --> 00:19:59,640 all the data should be copied to the memory 365 00:19:59,640 --> 00:20:01,340 prior to entering S3. 366 00:20:01,780 --> 00:20:03,820 And after the machine wakes up, 367 00:20:04,500 --> 00:20:06,920 it should be restored to the CPU. 368 00:20:07,620 --> 00:20:09,420 So let's have a look at our beloved 369 00:20:09,420 --> 00:20:11,640 MSM code check enable bit. 370 00:20:12,380 --> 00:20:14,540 In normal execution, it is set. 371 00:20:15,300 --> 00:20:17,820 When going into S3, it is still set. 372 00:20:18,020 --> 00:20:20,940 When the machine wakes up and we come back from S3 373 00:20:20,940 --> 00:20:23,100 as the CPU lost its power, 374 00:20:23,300 --> 00:20:24,440 it is cleared. 375 00:20:24,640 --> 00:20:27,940 And at the very early stage of the boot process, 376 00:20:28,520 --> 00:20:33,140 there is some initialization code that initializes this bit. 377 00:20:33,140 --> 00:20:35,760 So let's have a look at the initialization code. 378 00:20:37,100 --> 00:20:40,760 So clearly we see the write MSR over here 379 00:20:41,740 --> 00:20:43,240 with a certain value. 380 00:20:43,520 --> 00:20:45,780 You'd have to believe us that it comes from the memory. 381 00:20:46,220 --> 00:20:47,760 And here's the interesting bit. 382 00:20:49,000 --> 00:20:52,700 This write MSR happens only if the condition 383 00:20:52,700 --> 00:20:54,160 in the if statement holds, 384 00:20:54,380 --> 00:20:58,380 meaning the MSMM feature control supported global variable 385 00:20:58,380 --> 00:21:00,060 should not be zero. 386 00:21:01,200 --> 00:21:04,520 And remember we have a write primitive to the SMRAM, 387 00:21:04,620 --> 00:21:07,700 to the place where this global variable is stored. 388 00:21:09,360 --> 00:21:13,780 This leads to the full recipe of unauthorized code execution 389 00:21:13,780 --> 00:21:15,740 in SMM from kernel. 390 00:21:16,220 --> 00:21:18,980 So first, we use our write primitive 391 00:21:18,980 --> 00:21:22,680 to annul the MSMM feature control supported global variable. 392 00:21:23,460 --> 00:21:26,660 Then we put the machine to sleep, wake it up. 393 00:21:26,800 --> 00:21:29,080 At this point, the function you saw earlier 394 00:21:29,080 --> 00:21:30,000 is executed. 395 00:21:30,220 --> 00:21:33,000 The condition in the if statement does not hold. 396 00:21:33,160 --> 00:21:36,080 And the SMAP-like mechanism is not enabled. 397 00:21:36,800 --> 00:21:37,400 Hooray. 398 00:21:38,680 --> 00:21:42,260 Now we create our own SMI handler entry point, 399 00:21:42,360 --> 00:21:45,380 the first code that will be executed upon entering SMM. 400 00:21:46,200 --> 00:21:48,940 And we use our write primitive once again 401 00:21:48,940 --> 00:21:53,000 to modify the value of the SMBase MSR in the safe state. 402 00:21:53,180 --> 00:21:56,380 And thus, we trigger an SMBase relocation. 403 00:21:57,400 --> 00:21:59,060 And we trigger an SMBase release 404 00:21:59,080 --> 00:21:59,520 of the SMI. 405 00:21:59,520 --> 00:22:01,180 And drum rolls, please. 406 00:22:02,580 --> 00:22:04,620 We have code execution in SMM. 407 00:22:05,740 --> 00:22:06,680 All right. 408 00:22:09,800 --> 00:22:10,740 Great success. 409 00:22:11,980 --> 00:22:14,020 So frankly and humbly speaking, 410 00:22:14,180 --> 00:22:15,600 this method is pretty awesome. 411 00:22:16,200 --> 00:22:17,640 But it gets even better. 412 00:22:18,480 --> 00:22:22,200 So the SMI handler entry point starts running in real mode, 413 00:22:22,360 --> 00:22:25,040 meaning that the page tables enforcements 414 00:22:25,040 --> 00:22:28,120 have not kicked in yet because, well, there is no paging. 415 00:22:28,120 --> 00:22:32,500 And as we crafted our own SMI handler entry point, 416 00:22:32,740 --> 00:22:36,280 we have full and unrestricted memory access 417 00:22:36,280 --> 00:22:38,020 to all of the RAM. 418 00:22:38,620 --> 00:22:42,200 So for instance, we do not mind the non-executable 419 00:22:42,200 --> 00:22:43,340 or read-only pages, 420 00:22:44,120 --> 00:22:48,300 or the fact that the paging mechanism is static, 421 00:22:48,560 --> 00:22:49,940 or the read-only, 422 00:22:50,840 --> 00:22:53,840 or the fact that the page table resides in read-only pages, 423 00:22:54,020 --> 00:22:55,540 or the non-executable stack, 424 00:22:55,660 --> 00:22:57,180 or any of these mitigations. 425 00:22:57,180 --> 00:22:59,380 This is pretty awesome. 426 00:23:00,100 --> 00:23:04,420 However, newer mitigations do hinder our exploitation. 427 00:23:04,820 --> 00:23:09,020 In particular, the S3 and SMBase log bits. 428 00:23:09,180 --> 00:23:10,420 When these bits are set, 429 00:23:11,140 --> 00:23:15,940 the CR3 register and the SMBase MSR 430 00:23:15,940 --> 00:23:18,280 cannot be modified until next reboot. 431 00:23:20,800 --> 00:23:25,140 So while those mitigations are not fully adopted yet 432 00:23:25,140 --> 00:23:26,380 in all platforms, 433 00:23:27,180 --> 00:23:29,280 we wanted to deal with them as well. 434 00:23:29,860 --> 00:23:32,680 And to do that, we had no lead at this moment. 435 00:23:32,880 --> 00:23:35,200 So we decided to dig up once again 436 00:23:35,200 --> 00:23:36,220 through the pages of history. 437 00:23:36,660 --> 00:23:40,360 And we stumbled upon this cache poisoning attack 438 00:23:40,360 --> 00:23:43,400 that was presented in 2009 by Rafal and Joanna. 439 00:23:43,700 --> 00:23:45,820 And using the cache poisoning technique, 440 00:23:46,180 --> 00:23:50,480 you basically are able to arbitrarily read and write 441 00:23:50,480 --> 00:23:52,540 to and from the SMRAM. 442 00:23:53,160 --> 00:23:55,540 So let's see how this mechanism works. 443 00:23:57,220 --> 00:24:00,380 When we try to write to the SMRAM from the kernel, 444 00:24:00,540 --> 00:24:02,060 we will stumble upon a barrier, 445 00:24:02,300 --> 00:24:04,700 which basically makes sense, right? 446 00:24:04,740 --> 00:24:06,380 We are not allowed to write to the SMRAM, 447 00:24:06,460 --> 00:24:08,480 so the memory transactions will be discarded. 448 00:24:09,280 --> 00:24:11,360 However, this was a little bit misleading. 449 00:24:11,720 --> 00:24:14,940 In practice, when we try to write to the SMRAM, 450 00:24:15,040 --> 00:24:16,520 we'll go through the cache first, 451 00:24:16,720 --> 00:24:19,080 and the data will be stored there first, 452 00:24:19,320 --> 00:24:22,040 and only then it will be written back to the SMRAM. 453 00:24:22,300 --> 00:24:27,160 So what if, before the data is written back, 454 00:24:27,180 --> 00:24:29,620 we will trigger an SMI, 455 00:24:29,660 --> 00:24:33,480 and the CPU will enter SMM state, 456 00:24:33,800 --> 00:24:35,600 the barrier will be removed, 457 00:24:35,800 --> 00:24:39,240 and with some luck, only then the writeback will occur, 458 00:24:39,420 --> 00:24:42,680 and will modify the content of the SMRAM. 459 00:24:43,460 --> 00:24:44,900 So this would work. 460 00:24:45,160 --> 00:24:48,400 But obviously, this is already mitigated. 461 00:24:48,960 --> 00:24:52,980 And this is due to a special MSR that is called SMRR, 462 00:24:53,180 --> 00:24:55,080 the System Management Range Register, 463 00:24:55,360 --> 00:24:57,020 and it does a simple thing. 464 00:24:57,180 --> 00:25:01,200 It basically disables the caching of the SMRAM. 465 00:25:02,020 --> 00:25:05,100 But we already know how to disable MSRs, right? 466 00:25:05,320 --> 00:25:08,680 So let's see the code that restores it from the sleep state. 467 00:25:09,060 --> 00:25:11,500 And this probably looks familiar, 468 00:25:11,780 --> 00:25:13,740 and this is why. 469 00:25:13,920 --> 00:25:17,860 So this is the line of code that actually restores the MSR, 470 00:25:17,980 --> 00:25:21,160 and once again, it is dependent upon a global. 471 00:25:21,620 --> 00:25:24,160 So what if we annul this global, 472 00:25:24,340 --> 00:25:26,880 set it to zero using one of our write primitives, 473 00:25:27,180 --> 00:25:28,600 we'll put the machine into sleep, 474 00:25:28,800 --> 00:25:29,960 we'll wake it up, 475 00:25:30,900 --> 00:25:32,460 and when we wake up, 476 00:25:32,520 --> 00:25:35,280 we will be able to write to the SMRAM directly 477 00:25:35,280 --> 00:25:38,400 by resurrecting from the dead the cache poisoning attack. 478 00:25:38,760 --> 00:25:40,620 So once again, success, 479 00:25:40,920 --> 00:25:43,580 and we can deal with even those newer mitigations. 480 00:25:44,920 --> 00:25:47,020 So this is a great time for a demo, 481 00:25:47,940 --> 00:25:49,480 and let's hope it will work. 482 00:25:52,880 --> 00:25:53,340 Okay. 483 00:25:54,940 --> 00:25:55,540 Okay. 484 00:25:56,200 --> 00:25:56,800 Perfect. 485 00:25:57,160 --> 00:25:58,660 So before we begin, 486 00:25:58,740 --> 00:26:00,520 please notice that Intel NUC logo. 487 00:26:01,300 --> 00:26:02,500 This logo... 488 00:26:04,080 --> 00:26:04,380 Okay. 489 00:26:10,100 --> 00:26:12,080 This logo is stored on the SPI flash 490 00:26:12,080 --> 00:26:14,320 along with the UEFI firmware. 491 00:26:15,340 --> 00:26:19,100 So if we are able to execute code inside SMM, 492 00:26:19,160 --> 00:26:21,100 we will be able to modify this logo. 493 00:26:21,260 --> 00:26:22,960 The reason for that is because, 494 00:26:23,340 --> 00:26:25,840 only SMM is accessible to the SPI flash. 495 00:26:26,160 --> 00:26:28,640 It is the only one that is able to write to it. 496 00:26:29,760 --> 00:26:31,960 So now it's time to elevate our privileges. 497 00:26:32,500 --> 00:26:34,960 We'll begin by crafting a new write primitive 498 00:26:34,960 --> 00:26:36,220 of the value of zero. 499 00:26:36,600 --> 00:26:39,240 Remember, we had a rather complex write primitives, 500 00:26:39,280 --> 00:26:41,060 and none of them was an actual zero. 501 00:26:41,380 --> 00:26:42,940 So by chaining a couple of them 502 00:26:42,940 --> 00:26:46,620 and modifying a couple of globals inside the SMRAM, 503 00:26:46,680 --> 00:26:48,500 we were able to craft the value of zero. 504 00:26:50,220 --> 00:26:52,360 And this will help up in the... 505 00:26:52,360 --> 00:26:54,300 in the next step of our attack. 506 00:26:54,540 --> 00:26:56,920 So now we will try to annul 507 00:26:56,920 --> 00:26:59,480 the MSMM feature control supported global, 508 00:26:59,780 --> 00:27:03,020 which is responsible for the MSR restoration. 509 00:27:04,620 --> 00:27:07,360 And if you are actually able to disable it, 510 00:27:07,360 --> 00:27:08,740 when we return from sleep state, 511 00:27:08,920 --> 00:27:10,560 the MSR will be annulled. 512 00:27:11,840 --> 00:27:15,420 So now that we have annulled this global, 513 00:27:16,140 --> 00:27:18,920 we will read the MSR before we go to sleep state. 514 00:27:19,040 --> 00:27:21,860 We will see that the value of the MSR 515 00:27:21,860 --> 00:27:25,160 is five, which stands for two enabled bits, 516 00:27:25,340 --> 00:27:27,460 both the enable bit and the lock bit. 517 00:27:27,920 --> 00:27:29,480 And we'll put the machine into sleep. 518 00:27:30,500 --> 00:27:31,700 So at this moment, 519 00:27:31,840 --> 00:27:33,740 we count on the user to return from the sleep. 520 00:27:33,940 --> 00:27:36,440 I guess no one was ever concerned 521 00:27:36,440 --> 00:27:38,340 that he's being SMM mega breached 522 00:27:38,340 --> 00:27:40,220 when his machine suddenly went to sleep. 523 00:27:41,200 --> 00:27:42,760 And now that he is back, 524 00:27:42,940 --> 00:27:44,620 we will read the MSR once again, 525 00:27:44,720 --> 00:27:45,440 and it is zero. 526 00:27:46,180 --> 00:27:49,400 So now we are able to execute code outside the SMRAM. 527 00:27:49,780 --> 00:27:51,840 But how we will do this? 528 00:27:51,860 --> 00:27:53,880 We will do it using the SMBase relocation attack. 529 00:27:54,120 --> 00:27:55,200 So at this point, 530 00:27:55,260 --> 00:27:57,340 we'll execute the SMBase relocation attack 531 00:27:57,750 --> 00:28:01,180 and execute our own shellcode outside the SMRAM. 532 00:28:01,650 --> 00:28:03,180 So what the shellcode actually does, 533 00:28:03,300 --> 00:28:06,360 it simply replaces one of the SMI handlers. 534 00:28:06,780 --> 00:28:10,140 And now we are only left with triggering it. 535 00:28:10,820 --> 00:28:13,740 So now that we have triggered it, 536 00:28:14,160 --> 00:28:15,580 we will reboot the machine 537 00:28:15,580 --> 00:28:19,720 and see the result of what our SMI handler actually did. 538 00:28:27,020 --> 00:28:27,800 And voila, 539 00:28:28,280 --> 00:28:29,740 we managed to change the logo. 540 00:28:38,760 --> 00:28:40,880 So to better understand the implications 541 00:28:40,880 --> 00:28:42,740 of the issues we have found, 542 00:28:42,880 --> 00:28:45,280 we need to understand the UEFI ecosystem first. 543 00:28:45,560 --> 00:28:47,960 So everything begins with the Tiana core project, 544 00:28:48,040 --> 00:28:50,340 or what some of you may refer to as the EDK2, 545 00:28:50,340 --> 00:28:52,300 or practically the source code 546 00:28:52,300 --> 00:28:54,260 for any of your modern UEFI frameworks. 547 00:28:55,020 --> 00:28:56,800 Then comes the IBVs, 548 00:28:56,800 --> 00:28:58,040 the independent BIOS vendors. 549 00:28:58,320 --> 00:29:00,720 They take this source code, EDK2, 550 00:29:00,880 --> 00:29:03,480 and practically add the major portion of functionality. 551 00:29:03,960 --> 00:29:06,960 They add all the different SMI handlers along the way. 552 00:29:07,740 --> 00:29:10,720 Then they deliver it to the OEMs, 553 00:29:10,720 --> 00:29:12,220 to the original equipment manufacturers, 554 00:29:12,580 --> 00:29:16,360 such as Acer, Asus, Dell, Gigabyte, HP, 555 00:29:16,600 --> 00:29:20,280 Intel in the case of the Intel NUCs, Lenovo, and MSI. 556 00:29:21,200 --> 00:29:23,900 Then those OEMs deliver it to you guys, 557 00:29:24,020 --> 00:29:24,560 the end customers. 558 00:29:25,180 --> 00:29:26,800 So in the attempt to understand 559 00:29:26,800 --> 00:29:29,140 how many devices are vulnerable to RingCopper, 560 00:29:29,340 --> 00:29:31,360 we found out that only in 2020 561 00:29:31,360 --> 00:29:34,480 there were manufactured more than 200 million devices 562 00:29:34,480 --> 00:29:36,120 that are vulnerable to our attack, 563 00:29:36,300 --> 00:29:37,680 which is absolutely crazy, 564 00:29:37,880 --> 00:29:41,500 and probably results in a couple of billion devices 565 00:29:41,500 --> 00:29:42,280 that are vulnerable. 566 00:29:43,560 --> 00:29:45,920 And obviously it means tons of CVEs. 567 00:29:46,380 --> 00:29:48,960 So if things were not bad enough, 568 00:29:49,100 --> 00:29:50,260 we want to make things easier. 569 00:29:50,280 --> 00:29:50,660 Even worse. 570 00:29:50,960 --> 00:29:52,020 Up until now, 571 00:29:52,200 --> 00:29:54,900 we attacked the machine from kernel 572 00:29:54,900 --> 00:29:57,540 to SMM, aka Ring-2. 573 00:29:57,740 --> 00:30:01,880 But we want to make our attack work from Ring-3 as well, 574 00:30:01,940 --> 00:30:02,680 from user space. 575 00:30:03,060 --> 00:30:03,840 And to do that, 576 00:30:03,860 --> 00:30:05,540 we require three different things. 577 00:30:05,820 --> 00:30:07,840 We need to be able to generate DMA transactions, 578 00:30:08,340 --> 00:30:10,380 we need to be able to trigger SMIs, 579 00:30:10,580 --> 00:30:13,360 and we need to be able to write to specific physical memory. 580 00:30:13,980 --> 00:30:16,280 So let's begin by generating DMA transactions. 581 00:30:17,200 --> 00:30:18,960 So remember the previous diagram 582 00:30:18,960 --> 00:30:22,640 where we hooked inside a kernel the DMA response, 583 00:30:23,100 --> 00:30:26,020 and we made some very complicated DMA, 584 00:30:26,120 --> 00:30:29,320 generated very complicated DMA requests? 585 00:30:30,100 --> 00:30:31,220 So figure that out. 586 00:30:31,220 --> 00:30:32,600 It is way, way simpler. 587 00:30:32,980 --> 00:30:34,960 In order to generate DMA requests, 588 00:30:35,480 --> 00:30:36,740 you only have to read the file. 589 00:30:37,180 --> 00:30:39,560 The data will be returned to us with DMA directly. 590 00:30:40,040 --> 00:30:44,100 And to make sure that it is being redirected 591 00:30:44,100 --> 00:30:44,980 to the input buffer, 592 00:30:45,180 --> 00:30:46,280 we do not need a hook. 593 00:30:46,460 --> 00:30:48,340 We simply read it into the input buffer. 594 00:30:48,960 --> 00:30:50,940 So this was it, 595 00:30:51,060 --> 00:30:53,460 and how we generated DMA transactions from user space. 596 00:30:53,700 --> 00:30:54,720 It was rather simple, 597 00:30:55,000 --> 00:30:58,620 but triggering SMIs is way complicated. 598 00:30:58,940 --> 00:31:02,720 And this is because we need to execute a special opcode, 599 00:31:02,860 --> 00:31:03,540 outb2, 600 00:31:03,780 --> 00:31:06,500 that can be executed only from kernel. 601 00:31:07,340 --> 00:31:08,820 And at this point, 602 00:31:08,920 --> 00:31:10,360 we got very frustrated. 603 00:31:10,560 --> 00:31:12,840 And like every frustrated millennial, 604 00:31:12,960 --> 00:31:14,320 we went on a social media. 605 00:31:14,640 --> 00:31:15,900 And in specific, 606 00:31:16,100 --> 00:31:18,160 the ones that if you triple its name by, 607 00:31:18,160 --> 00:31:19,080 by three, 608 00:31:19,160 --> 00:31:19,840 by three, 609 00:31:19,900 --> 00:31:22,380 you'll probably get the wrong results. 610 00:31:24,160 --> 00:31:25,300 So anyway, 611 00:31:27,760 --> 00:31:30,880 we stumbled upon one of Alex Matrosov's tweets, 612 00:31:31,040 --> 00:31:32,120 which says that, 613 00:31:32,140 --> 00:31:32,720 practically, 614 00:31:33,000 --> 00:31:36,540 any BIOS update tools that you'll find out there, 615 00:31:36,660 --> 00:31:39,080 probably has some low-level functionality 616 00:31:39,080 --> 00:31:42,100 that can be reused for offensive purposes. 617 00:31:42,700 --> 00:31:43,900 And indeed there was. 618 00:31:45,020 --> 00:31:46,200 So we, 619 00:31:46,320 --> 00:31:47,540 AMI provides two drivers. 620 00:31:48,160 --> 00:31:50,200 A Linux driver and a signed Windows driver. 621 00:31:50,480 --> 00:31:53,620 And both of them expose an API for triggering 622 00:31:53,620 --> 00:31:55,700 any arbitrary SMIs that we desire. 623 00:31:56,220 --> 00:31:57,460 Basically a octal. 624 00:31:59,100 --> 00:32:02,440 So now that we have done with triggering SMIs, 625 00:32:02,520 --> 00:32:05,420 we are only left with writing to specific physical memory. 626 00:32:05,940 --> 00:32:07,280 And I remind you, 627 00:32:07,300 --> 00:32:10,200 we wanted to make it work both from Windows and Linux. 628 00:32:10,500 --> 00:32:12,360 So let's start with Windows. 629 00:32:14,860 --> 00:32:17,240 So the communication with SMM 630 00:32:17,240 --> 00:32:20,220 is a little bit more complicated. 631 00:32:20,540 --> 00:32:23,120 It has a special structure that is called the mailbox, 632 00:32:23,420 --> 00:32:26,620 which has a description of all the things related 633 00:32:26,620 --> 00:32:28,120 with the communication with SMM. 634 00:32:28,180 --> 00:32:29,360 And among others, 635 00:32:29,440 --> 00:32:32,420 it contains a physical address of a row buffer, 636 00:32:32,620 --> 00:32:34,920 which would actually contain the input. 637 00:32:35,460 --> 00:32:37,920 So in order to access it from SMM, 638 00:32:37,960 --> 00:32:39,840 we require a physical address, 639 00:32:40,080 --> 00:32:44,140 which is basically the virtual address within SMM context. 640 00:32:44,420 --> 00:32:46,680 And to access it from user space, 641 00:32:46,680 --> 00:32:49,020 we require a virtual address 642 00:32:49,020 --> 00:32:52,740 that maps exactly this physical memory region. 643 00:32:53,100 --> 00:32:56,140 So let's look at the buffer mapping the octal 644 00:32:56,140 --> 00:32:57,860 inside one of the Windows drivers. 645 00:32:58,360 --> 00:33:01,140 So it begins by triggering SMID9, 646 00:33:01,360 --> 00:33:03,840 which basically populates the mailbox 647 00:33:03,840 --> 00:33:07,120 and along the way the row pointer to the buffer. 648 00:33:08,300 --> 00:33:11,260 And then in order to access this mailbox, 649 00:33:11,600 --> 00:33:14,780 it needs to map this physical address. 650 00:33:14,980 --> 00:33:16,660 So the next thing that you need to do 651 00:33:16,680 --> 00:33:18,420 is basically map this physical address 652 00:33:18,420 --> 00:33:20,700 into an accessible virtual address. 653 00:33:21,260 --> 00:33:23,560 Then at offset eight, 654 00:33:23,740 --> 00:33:27,500 it access the pointer to this physical buffer, 655 00:33:27,660 --> 00:33:28,180 the row buffer, 656 00:33:28,420 --> 00:33:32,760 and once again map it into a user accessible virtual address, 657 00:33:33,780 --> 00:33:36,720 and then returns everything into a user accessible memory 658 00:33:36,720 --> 00:33:40,080 so the user will be able to access those virtual addresses. 659 00:33:41,180 --> 00:33:44,200 So some of you may notice there is a race condition here. 660 00:33:44,400 --> 00:33:46,540 In between the time of the address 661 00:33:46,680 --> 00:33:47,660 that the SMI is being triggered 662 00:33:47,660 --> 00:33:49,320 and the mailbox is being populated 663 00:33:49,320 --> 00:33:52,880 with the pointer to this row buffer 664 00:33:53,520 --> 00:33:56,760 and the times it is being mapped to user virtual address. 665 00:33:57,040 --> 00:33:59,100 So what if we change this physical address 666 00:33:59,100 --> 00:34:02,680 in between the time that the mailbox is being populated 667 00:34:02,680 --> 00:34:05,760 and the times that this physical address is being mapped, 668 00:34:06,020 --> 00:34:08,580 basically we'll be able to map 669 00:34:08,580 --> 00:34:10,960 any arbitrary physical address that we desire. 670 00:34:12,540 --> 00:34:14,220 So let's see how it works. 671 00:34:14,700 --> 00:34:16,600 We will trigger the buffer mapping the octal twice. 672 00:34:16,680 --> 00:34:17,640 On the first attempt, 673 00:34:17,820 --> 00:34:21,040 we will get an access to the mailbox virtual address. 674 00:34:21,180 --> 00:34:23,600 So we will be able to modify the mailbox at offset eight 675 00:34:23,600 --> 00:34:25,380 with our own desired physical address. 676 00:34:25,780 --> 00:34:28,020 And then we will modify the mailbox 677 00:34:28,020 --> 00:34:29,220 to exploit this race condition 678 00:34:29,220 --> 00:34:31,360 and basically change this address 679 00:34:31,360 --> 00:34:33,120 with our own arbitrary physical address 680 00:34:33,540 --> 00:34:36,820 and we will get the corresponding virtual address. 681 00:34:37,420 --> 00:34:39,080 So this looks great, 682 00:34:39,260 --> 00:34:41,520 but reality makes things even harder. 683 00:34:41,720 --> 00:34:43,820 In order to avoid the remapping 684 00:34:43,820 --> 00:34:46,500 of the same physical addresses over and over again, 685 00:34:46,680 --> 00:34:48,520 those globals are being checked, 686 00:34:48,740 --> 00:34:50,760 which basically makes sense, right? 687 00:34:50,860 --> 00:34:53,020 We do not want to map the same address 688 00:34:53,020 --> 00:34:53,920 over and over again. 689 00:34:55,700 --> 00:34:56,900 So at this point, 690 00:34:56,920 --> 00:34:58,820 we decided to look at the cleanup octal. 691 00:34:59,120 --> 00:35:02,640 And we noticed that the cleanup octal, 692 00:35:02,680 --> 00:35:05,900 all it does is to annul those globals. 693 00:35:05,980 --> 00:35:06,760 And I will repeat it, 694 00:35:06,800 --> 00:35:08,260 it only annuls the globals. 695 00:35:08,380 --> 00:35:12,240 There is no unmapping of this virtual address once or ever. 696 00:35:12,380 --> 00:35:14,800 So it is still accessible to user space. 697 00:35:14,800 --> 00:35:16,900 So we will use this fact 698 00:35:16,900 --> 00:35:19,160 and we will make the following exploitation. 699 00:35:19,840 --> 00:35:22,060 So we will trigger the buffer mapping octal 700 00:35:22,060 --> 00:35:23,880 to get a mailbox virtual address. 701 00:35:24,300 --> 00:35:25,900 We will trigger the cleanup octal 702 00:35:25,900 --> 00:35:26,940 to annul the globals. 703 00:35:27,580 --> 00:35:28,880 We will exploit the file 704 00:35:28,880 --> 00:35:30,500 and then we will exploit the following race condition. 705 00:35:31,700 --> 00:35:32,660 From two cores, 706 00:35:32,900 --> 00:35:33,780 we will do as follows. 707 00:35:33,940 --> 00:35:34,480 On the first core, 708 00:35:34,700 --> 00:35:36,540 we will trigger the buffer mapping octal 709 00:35:36,540 --> 00:35:38,340 once again to populate the mailbox 710 00:35:38,340 --> 00:35:41,860 and to map the physical address inside it. 711 00:35:42,460 --> 00:35:43,780 From within the other cores, 712 00:35:43,780 --> 00:35:46,700 we will use our previous virtual address of the mailbox 713 00:35:46,700 --> 00:35:49,960 to modify the top state 714 00:35:49,960 --> 00:35:52,380 with our own desired physical address. 715 00:35:53,280 --> 00:35:55,520 Then we will check if the newly mapped physical address 716 00:35:55,520 --> 00:35:56,560 is the one that we wanted. 717 00:35:57,140 --> 00:35:57,860 If not, 718 00:35:58,080 --> 00:35:59,980 we will simply repeat the entire process. 719 00:36:00,260 --> 00:36:01,980 We will trigger the cleanup octal, 720 00:36:02,000 --> 00:36:03,460 repeat the entire process once again 721 00:36:03,460 --> 00:36:04,720 until we succeed. 722 00:36:07,340 --> 00:36:10,540 So you folks are pretty tired by now, I guess. 723 00:36:11,680 --> 00:36:13,580 We presented a lot of info, 724 00:36:13,580 --> 00:36:17,460 but do you have a moment to talk about Linux? 725 00:36:19,430 --> 00:36:23,980 AMI lets us do some pretty amazing things from user space. 726 00:36:25,290 --> 00:36:30,560 Here we open the kernel module 727 00:36:30,560 --> 00:36:34,200 and trigger the mmap syscall 728 00:36:34,200 --> 00:36:40,000 with the kernel module's file descriptor 729 00:36:40,000 --> 00:36:42,500 to map any physical address, 730 00:36:42,500 --> 00:36:43,840 which is pretty awesome. 731 00:36:44,240 --> 00:36:45,380 At this point, 732 00:36:45,440 --> 00:36:49,580 we have to somehow be able to generate DMA transactions 733 00:36:49,580 --> 00:36:51,040 for our exploit to work. 734 00:36:51,200 --> 00:36:52,880 So just as we did on Windows, 735 00:36:53,160 --> 00:36:55,540 we'll simply open a file, 736 00:36:56,540 --> 00:37:00,280 read it to the previously mapped memory, 737 00:37:00,440 --> 00:37:01,740 and hope for the best. 738 00:37:03,220 --> 00:37:07,360 From the Linux module's side, 739 00:37:09,600 --> 00:37:12,040 they initialize the mmap attribute 740 00:37:12,040 --> 00:37:13,460 of the file operations 741 00:37:13,460 --> 00:37:18,040 struct with their own function, 742 00:37:18,520 --> 00:37:24,080 which actually does the physical-to-virtual mapping 743 00:37:24,080 --> 00:37:25,860 using the remap-pfn range, 744 00:37:26,000 --> 00:37:27,100 which is pretty straightforward. 745 00:37:27,480 --> 00:37:29,100 It gets a physical address 746 00:37:29,100 --> 00:37:32,860 and returns a user-accessible virtual address. 747 00:37:33,720 --> 00:37:35,040 But there are some bad news. 748 00:37:36,820 --> 00:37:38,500 When we execute this code, 749 00:37:38,680 --> 00:37:40,080 we get an e-fault. 750 00:37:40,080 --> 00:37:42,040 And the reason for that 751 00:37:42,040 --> 00:37:45,660 is in the way remap-pfn range works. 752 00:37:45,980 --> 00:37:47,620 When running this function, 753 00:37:48,040 --> 00:37:51,280 page ranges are managed without struct page, 754 00:37:51,460 --> 00:37:52,960 just pure pfn. 755 00:37:53,140 --> 00:37:56,620 Simply put, this will not work. 756 00:37:56,780 --> 00:37:59,840 No DMA transactions will be generated here. 757 00:38:00,840 --> 00:38:03,080 However, we have the ability 758 00:38:03,080 --> 00:38:05,960 to map any physical address. 759 00:38:06,420 --> 00:38:08,720 Any physical address. 760 00:38:08,720 --> 00:38:11,360 So let's take the code from before, 761 00:38:11,460 --> 00:38:12,440 and instead of mapping 762 00:38:12,440 --> 00:38:14,220 some arbitrary physical address, 763 00:38:14,420 --> 00:38:15,660 let's map, for instance, 764 00:38:15,960 --> 00:38:17,400 the address of the Linux kernel 765 00:38:17,400 --> 00:38:19,220 or a kernel module. 766 00:38:21,180 --> 00:38:22,500 At this point, 767 00:38:22,500 --> 00:38:24,700 that we have the user-accessible mapping, 768 00:38:25,640 --> 00:38:29,480 we will be able to patch ring-zero code 769 00:38:29,480 --> 00:38:31,680 and gain code execution in kernel, 770 00:38:33,760 --> 00:38:35,600 which is pretty awesome. 771 00:38:35,680 --> 00:38:38,500 But it sounds kind of familiar, right? 772 00:38:38,720 --> 00:38:41,120 Do we remember how we generated 773 00:38:41,120 --> 00:38:42,520 DMA transactions from kernel? 774 00:38:43,620 --> 00:38:46,440 So now that we have all three bullets 775 00:38:46,440 --> 00:38:49,520 working from ring-three, 776 00:38:49,700 --> 00:38:52,840 we have a full exploitation chain 777 00:38:52,840 --> 00:38:54,820 from user-land to SMM, 778 00:38:55,720 --> 00:38:57,180 which concludes our journey. 779 00:38:57,620 --> 00:39:01,480 We first got the ability 780 00:39:01,480 --> 00:39:03,520 to write to the SMRAM. 781 00:39:03,720 --> 00:39:05,920 Then we tried to leverage it 782 00:39:05,920 --> 00:39:08,540 into code execution in SMM 783 00:39:08,720 --> 00:39:11,740 using the S3 boot script, 784 00:39:12,610 --> 00:39:14,680 and we failed spectacularly. 785 00:39:14,760 --> 00:39:18,680 And we successfully ran unauthorized code 786 00:39:18,680 --> 00:39:21,120 in SMM using and abusing 787 00:39:21,120 --> 00:39:24,160 the SMBase relocation technique. 788 00:39:24,860 --> 00:39:28,380 And lastly, we elevated the attack 789 00:39:28,380 --> 00:39:29,780 to work from user-land, 790 00:39:30,040 --> 00:39:32,320 and thus we managed to hop 791 00:39:32,320 --> 00:39:35,480 from user-space to SMM. 792 00:39:36,320 --> 00:39:38,100 We were Jonathan and Benny. 793 00:39:38,100 --> 00:39:39,680 Thank you so much 794 00:39:39,680 --> 00:39:41,000 for taking part in this journey.