1 00:00:01,420 --> 00:00:10,520 Hi and welcome to our talk Backdoor in the Core. My name is Alexander Krogh and I'm a 2 00:00:10,520 --> 00:00:17,240 vulnerability researcher at Vectorize. Besides that I'm also a CTF player and 3 00:00:17,240 --> 00:00:23,200 through that community that's where I learned this wonderful guy. We've been 4 00:00:23,200 --> 00:00:28,720 playing together for a few years now and yeah this is our talk about microcode. 5 00:00:28,720 --> 00:00:33,240 He's a grad student at the Danish Technical University and it's about 6 00:00:33,240 --> 00:00:40,020 microcode and hacking CPUs. But first before we dig into the microcode itself 7 00:00:40,020 --> 00:00:46,300 need a little crash course on computer architecture. So what we see up here is a 8 00:00:46,300 --> 00:00:53,520 CPU. It's a CPU core from the Skylake processors. Inside of it we have both the 9 00:00:53,520 --> 00:00:58,500 front end that's the up top and we have the back end down beneath it and some 10 00:00:58,500 --> 00:00:58,700 memory. 11 00:00:58,720 --> 00:01:03,960 Subsystem in the bottom. The front end is mostly in charge of doing stuff like 12 00:01:03,960 --> 00:01:10,300 fetching new instructions so that's where your your high level saying high 13 00:01:10,300 --> 00:01:16,340 level for instructions here that's the x86 instructions going in there into 14 00:01:16,340 --> 00:01:20,440 the fetch queue and they are getting decoded and then they will be 15 00:01:20,440 --> 00:01:24,980 translated into micro operations and micro operations is what's actually 16 00:01:24,980 --> 00:01:26,940 running on the CPU. 17 00:01:26,940 --> 00:01:34,000 And micro operations are then scheduled by the back end. The back end is the ALU so 18 00:01:34,000 --> 00:01:38,620 the arithmetic logic unit and floating point unit and stuff like that and 19 00:01:38,620 --> 00:01:42,580 through that the resulting stuff can go to memory for the memory subsystem. We 20 00:01:42,580 --> 00:01:47,380 are only going to take a very tiny section of this CPU and they dive into 21 00:01:47,380 --> 00:01:51,760 it today. So the one we are diving into is this little tiny section it's 22 00:01:51,760 --> 00:01:56,700 instruction queue and the decoding phase. So here when a instruction comes in and 23 00:01:56,940 --> 00:02:00,880 it goes through the queue and if it's a very simple instruction like just a 24 00:02:00,880 --> 00:02:05,780 simple move or addition XOR something simple like that it will go straight 25 00:02:05,780 --> 00:02:10,220 through the simple decoders and they have a one-to-one sort of mapping 26 00:02:10,220 --> 00:02:13,960 between micro operations and the high level macro operations. So we call it 27 00:02:13,960 --> 00:02:19,640 macro operations and micro operations from the ones going deep in the CPU. So 28 00:02:19,640 --> 00:02:24,680 more complex instructions will go through the complex decoder and what 29 00:02:24,680 --> 00:02:26,840 we're looking at today is the really heavy 30 00:02:26,840 --> 00:02:32,600 instructions. So that's something like return from a system call or like when 31 00:02:32,600 --> 00:02:37,280 you enter a hypervised space. Some of these instructions are multiple hundreds 32 00:02:37,280 --> 00:02:42,160 of lines long or multiple hundred micro operations long. So they can't just be 33 00:02:42,160 --> 00:02:46,460 directly decoded into our micro operations. They go to the ROM and on 34 00:02:46,460 --> 00:02:52,600 the ROM we fetch yeah we have like kind of like our normal CPU. We have micro 35 00:02:52,600 --> 00:02:56,460 codes so you have an instruction pointer running through an 36 00:02:56,840 --> 00:03:02,280 executing code and today we're looking at that space. So let's look so yeah so 37 00:03:02,280 --> 00:03:09,600 what why do we need this? Why do we need this complex set of micro code? Well 38 00:03:09,600 --> 00:03:14,140 first of all we have the complex instructions. Those cannot be directly 39 00:03:14,140 --> 00:03:21,860 implemented transistors but also bugs do occur in CPUs. So we we had like a 40 00:03:21,860 --> 00:03:25,800 Spectrum meltdown a few years back setting the scene of all of this and just 41 00:03:25,800 --> 00:03:26,620 a few weeks ago. 42 00:03:26,840 --> 00:03:31,360 We saw the simply bug. So more and more of these kind of CPU bugs are 43 00:03:31,360 --> 00:03:37,820 CPU bugs are popping up here and there and to fix these bugs post-production we 44 00:03:37,820 --> 00:03:42,960 need micro code updates. So besides the ROM area where micro code is stored we 45 00:03:42,960 --> 00:03:47,740 also have a small RAM address space and that is writable and that's what we're 46 00:03:47,740 --> 00:03:53,460 going to look at today. Normally these updates are assigned and encrypted by 47 00:03:53,460 --> 00:03:55,320 Intel so you can't really 48 00:03:55,980 --> 00:04:01,480 have that deep dive and go look what's actually pushed by Intel to the CPUs. 49 00:04:01,600 --> 00:04:07,600 There hasn't really been a way so far to like inspect that and we'll see what 50 00:04:07,600 --> 00:04:13,440 kind of damage we can do with that if we actually get the access. So this is a 51 00:04:13,440 --> 00:04:18,960 table showing how the address space is laid out inside of these CPUs. It's a 52 00:04:18,960 --> 00:04:25,260 very simple address space. It's totally linear. So it goes from zero down to a 53 00:04:25,320 --> 00:04:31,920 0x800 or 8000 sorry and somewhere in the middle we see a split going from 54 00:04:31,920 --> 00:04:38,640 read-only at 7c00. That's where the writable address space starts. So that's 55 00:04:38,640 --> 00:04:46,020 where we can inject micro code and out on the columns we see the instructions 56 00:04:46,020 --> 00:04:49,640 themselves. They are we have grouped them together in this table because that's 57 00:04:49,640 --> 00:04:55,300 how they're grouped in the CPU. We call they're called triads. So I'm going to show you a 58 00:04:55,320 --> 00:05:00,740 one triad is three instructions and one implicit knob instruction so every fourth instructions 59 00:05:00,740 --> 00:05:05,420 would just be an implicit knob you can't address them you can't jump to them but they won't 60 00:05:05,420 --> 00:05:10,240 really execute anything besides that we have the last thing and that's a secret 61 00:05:10,240 --> 00:05:16,340 sequence word it's stored separate separately from the instructions and the second word is 62 00:05:16,340 --> 00:05:23,780 what groups these uh tries together they are the ones in charge of doing um flow controls so they 63 00:05:23,780 --> 00:05:28,540 can like do branching based on testings test instructions and stuff like that 64 00:05:30,280 --> 00:05:37,200 uh so let's take the first example of how um a instruction could be implemented in microcode 65 00:05:37,540 --> 00:05:44,300 this is the x adder instruction it's exchange and addition in the same time no normally we 66 00:05:44,640 --> 00:05:51,800 see these x86 instructions as a one atomic thing but they're actually not so this x addition 67 00:05:51,800 --> 00:05:53,120 is 68 00:05:53,780 --> 00:06:00,380 supposed of these three micro operations um so let's read through it so the first thing happening 69 00:06:00,380 --> 00:06:07,140 is that we take uh the source register in this case on the first line and or it with a zero the 70 00:06:07,140 --> 00:06:15,220 source register is the rbx um in this case because it's on that side and rax is the destination 71 00:06:15,220 --> 00:06:22,360 register so oring with a zero that just means like that's basically just a move we move it into 72 00:06:23,780 --> 00:06:30,040 a zero is a physical and temporary register inside the cpu that you normally can't access 73 00:06:30,720 --> 00:06:37,980 um yeah so that's a simple move then we do a syracuse zero extent uh but in this case it's 74 00:06:37,980 --> 00:06:45,580 64 bits so it's basically just a again just a move so we move it into the uh r64 source register 75 00:06:46,160 --> 00:06:53,460 so that's basically taking rax and putting into rdx um so that's the exchange part of this instruction 76 00:06:54,100 --> 00:06:59,220 after that we do the final step and that's the addition so we take the safe temporary register 77 00:06:59,700 --> 00:07:06,940 and add it back into the destination register and we also store the resulting value in the 78 00:07:06,940 --> 00:07:13,100 destination register and now at last we see the final part that's the sequence word coming into 79 00:07:13,100 --> 00:07:20,300 play and in this case it's a simple u end and that means that the instruction the macro instruction 80 00:07:20,300 --> 00:07:23,200 the x add will end at this point 81 00:07:23,780 --> 00:07:28,260 so we will go and update uh the instruction pointer and fetch a new one 82 00:07:29,340 --> 00:07:33,740 now simple example so what you saw before what's the temp 0 register 83 00:07:34,260 --> 00:07:40,840 that's a new one to most of us that's a temporary register only used inside microcode 84 00:07:40,840 --> 00:07:48,780 and it's not visible to um yeah to the macro code and we got 16 of these register 0 through 15 85 00:07:48,780 --> 00:07:53,620 they called um of course my microcode can also access all of the normal register 86 00:07:53,780 --> 00:08:00,300 registers, like RX, RBX, and stuff like that. Besides that, we got eight floating-point 87 00:08:00,300 --> 00:08:06,500 registers, just same thing as the temp registers, there's eight of them for XMM instructions, 88 00:08:06,860 --> 00:08:15,240 also temporarily. Then we can of course access system registers, that can be like segmentation 89 00:08:15,240 --> 00:08:23,000 of memory, some of those registers, they're the ones that holds the state. And last we 90 00:08:23,000 --> 00:08:29,420 can, we'll hold what modes the CPUs are running in, and all the critical stuff, so like are 91 00:08:29,420 --> 00:08:34,180 we in 32-bit mode, are we in 64-bit mode, are we in a hyper-vast space, and stuff like 92 00:08:34,180 --> 00:08:40,700 that. And often the CPU will check that flag, and do conditional jumps in microcode based 93 00:08:40,700 --> 00:08:45,100 on what state are we in, are we in privilege mode, are we in kernel space, stuff like that. 94 00:08:46,300 --> 00:08:51,780 Of course, from microcode we can also access memory. There's multiple ways of doing this, 95 00:08:51,880 --> 00:08:52,980 one is for example, we can access memory, we can access memory, we can access memory, 96 00:08:52,980 --> 00:08:57,760 for example, the virtual address space, the ones we usually do from macro operations, 97 00:08:58,040 --> 00:09:06,020 so we can store and read memory, just as that. We can also access directly from this physical 98 00:09:06,020 --> 00:09:12,060 address space, going just around all the page tables and stuff like that. So, yeah. 99 00:09:12,640 --> 00:09:20,460 And besides that, we have some very, very tiny memory, dedicated for only microcode 100 00:09:20,760 --> 00:09:21,960 and inside the CPU. 101 00:09:21,960 --> 00:09:27,540 That's the U-code memory. It's a separate address space from where we are storing the 102 00:09:27,540 --> 00:09:33,280 actual instructions. So, data and instructions are separate completely, but here we have 103 00:09:33,280 --> 00:09:41,440 around 0x100 keywords that we can also use as temporary storage. Each entry is 64-bit 104 00:09:41,440 --> 00:09:47,280 wide, and it is used, the difference between those and temporary instructions is that these 105 00:09:47,280 --> 00:09:50,480 can be used across multiple macro instructions. 106 00:09:50,480 --> 00:09:56,680 So, temp registers is for temporary storage inside one macro instructions, and this can 107 00:09:56,680 --> 00:10:01,880 store safe control registers when switching mode and stuff like that, and then we can 108 00:10:01,880 --> 00:10:10,100 fetch it out later. So, each entry is like, has a dedicated purpose. Other than that, 109 00:10:10,240 --> 00:10:17,540 we also have two buses, or a bus and a fabric. These are used for communication within the 110 00:10:17,540 --> 00:10:17,840 CPU. 111 00:10:18,820 --> 00:10:20,460 One of the very interesting ones is the one that we have here, which is the one that we 112 00:10:20,460 --> 00:10:26,320 call the control register bus. It can talk to all components inside one single CPU core. 113 00:10:26,660 --> 00:10:33,060 That could be the caches that are on the core. So, for example, an L1 cache, maybe an L2, 114 00:10:33,160 --> 00:10:38,400 something like that, and besides that, we have the very important one, the microcode 115 00:10:38,400 --> 00:10:45,480 sequencer. That is the one in charge of scheduling instructions and going through the pipelines. 116 00:10:45,700 --> 00:10:50,260 So, if we can go to the control register bus, we can access the microcode as well. 117 00:10:51,440 --> 00:10:57,300 Other than that, we have the insole-on system fabric. That is mostly used for external communication. 118 00:10:57,880 --> 00:11:04,840 So, the HTT driver and USB stuff and other components that are shared between some of 119 00:11:04,840 --> 00:11:10,160 the cores. So, that is mostly for external communication outside of the main core. 120 00:11:10,880 --> 00:11:17,500 Yes? Okay. So, now we talked a bit about that. How do we place these updates in microcode? 121 00:11:17,660 --> 00:11:20,440 So, we talked about that it's a ROM area, and it's also a memory area. So, we talked 122 00:11:20,460 --> 00:11:24,500 about that it's a ROM area, and that is read-only. So, when Intel pushes an update, 123 00:11:24,680 --> 00:11:26,800 how do they actually update a ROM area? 124 00:11:27,140 --> 00:11:32,860 That's where these match and patch registers come into place. What we see here is the 125 00:11:32,860 --> 00:11:39,220 bit fields and how a match and patch register is laid out. And we have 32 of these, and 126 00:11:39,220 --> 00:11:45,520 each time with a new instruction, we will check these fields, and if we get a hit, we 127 00:11:45,520 --> 00:11:50,160 will jump to that address instead. So, we have the present bit. That will tell if this match and patch 128 00:11:50,460 --> 00:11:55,980 is enabled then we have a source field that is if we hit this address go jump 129 00:11:55,980 --> 00:12:00,900 somewhere else and that will be what's stored in the destination and as you see 130 00:12:00,900 --> 00:12:07,200 up here both source and destination are to you at us so they are microcode 131 00:12:07,200 --> 00:12:14,480 addresses but they shifted by one so one lose one bit of position and we'll talk 132 00:12:14,480 --> 00:12:19,560 a bit about how that works so so here we have an example of a matching patch 133 00:12:19,560 --> 00:12:24,420 register so let's say our CPU is running and the microcode instruction 134 00:12:24,420 --> 00:12:30,340 pointer hits free c8 then we have the matching patch hit because we see a 135 00:12:30,340 --> 00:12:34,800 entry in the matching patch table with the same value that means that we will 136 00:12:34,800 --> 00:12:41,040 instead go and jump directly to 7c 0 0 and start executing from there instead 137 00:12:41,040 --> 00:12:46,780 and this is what Intel programs into the CPU every time we apply a microcode 138 00:12:46,780 --> 00:12:49,140 update which happens every on every reboot 139 00:12:49,140 --> 00:12:55,460 but we also find out through trial and error that this loss in position means 140 00:12:55,460 --> 00:13:03,020 that if we do his free c9 we would actually also get a hit and then we'll 141 00:13:03,020 --> 00:13:07,860 jump jump to the red area instead so we will jump one micro instruction further 142 00:13:07,860 --> 00:13:16,500 on so how can we use this or should I say abuses when researching CPUs so a 143 00:13:16,500 --> 00:13:18,780 lot of things we have been doing is to do dynamic 144 00:13:19,140 --> 00:13:24,140 inspection of state just to fake also to figure out what this code doing but 145 00:13:24,140 --> 00:13:28,300 also to figure out what's instructions doing so here we have a simple example 146 00:13:28,300 --> 00:13:34,740 taken from the ROM it reads first from the RAM address space into temp 3 and 147 00:13:34,740 --> 00:13:39,800 temp 2 but what if you want to inspect the state well one thing we could do is 148 00:13:39,800 --> 00:13:43,580 that we can tell the matching patch registers to come go and jump somewhere 149 00:13:43,580 --> 00:13:48,900 else so we see the the all over crossed line the red line that is 150 00:13:49,140 --> 00:13:53,600 the test use date and then normally we would the sequence word will tell it to 151 00:13:53,600 --> 00:13:58,700 jump somewhere to the general protection fault and do a fault because this is a 152 00:13:58,700 --> 00:14:03,540 privileged instruction but we can change that so we swap that out and say 153 00:14:03,540 --> 00:14:09,700 go to the RAM area and in the RAM we have put our stuff and we will flip the 154 00:14:09,700 --> 00:14:16,880 test case and when we do that we can tell it to either go and take the left 155 00:14:16,880 --> 00:14:17,940 part and say 156 00:14:17,940 --> 00:14:18,720 move 157 00:14:18,720 --> 00:14:24,020 , move these temporary registers into the interacts and rbx these are registers 158 00:14:24,020 --> 00:14:31,980 that we can inspect from from a debugger and from normal user space or if we are 159 00:14:31,980 --> 00:14:36,780 in a privileged mode we don't want to screw up this instruction so instead we 160 00:14:36,780 --> 00:14:40,900 send it to its normal path and send it back the way we came from so we don't 161 00:14:40,900 --> 00:14:44,780 mess up the instruction and the CPU will still continue to function and run 162 00:14:44,780 --> 00:14:47,600 happily ever after okay 163 00:14:47,600 --> 00:14:52,380 so how do we manage to make these change now how can we access the micro 164 00:14:52,380 --> 00:14:56,760 code sequencer when it's all locked down and locked up by Incil well luckily the 165 00:14:56,760 --> 00:15:03,880 people at positive technology they found a vulnerability in the Incil management 166 00:15:03,880 --> 00:15:09,580 engine that runs a software called trusted execution unit and they found a 167 00:15:09,580 --> 00:15:17,580 buffer overflow and that engine runs on a very highly privileged level and through that we can enable debug features 168 00:15:17,580 --> 00:15:17,760 Features. 169 00:15:17,940 --> 00:15:26,920 Debug features only, Intel is supposed to have access to, and that unlocks hidden instructions 170 00:15:26,920 --> 00:15:28,400 and undocumented instructions. 171 00:15:29,480 --> 00:15:35,840 So yeah, we took the POC and expanded it to these dev boards. 172 00:15:36,020 --> 00:15:39,440 That's actually the board lying up here that we are presenting from. 173 00:15:40,200 --> 00:15:45,160 And we have, of course, prepared flash images and stuff that you can flash on, so if you 174 00:15:45,160 --> 00:15:48,520 buy one of these, you can go and replicate some of our findings. 175 00:15:50,720 --> 00:15:57,300 The two instructions that we mostly use is the uDebugRead and the uDebugWrite. 176 00:15:57,660 --> 00:16:06,580 These two instructions can write to microcode, it can read and write to the data areas we 177 00:16:06,580 --> 00:16:07,060 talked before. 178 00:16:07,580 --> 00:16:12,420 The Write instruction also has a special feature, where you can put in an address in the RACS 179 00:16:12,420 --> 00:16:15,000 register and just tell it to go jump straight. 180 00:16:15,000 --> 00:16:18,100 So an address you specify in the microcode area. 181 00:16:18,580 --> 00:16:23,520 And it can also talk to the control register bus that we talked about before. 182 00:16:23,800 --> 00:16:26,820 So that's how we access all of these microcode features. 183 00:16:28,480 --> 00:16:31,900 Really good findings over at the post-exam early side. 184 00:16:33,320 --> 00:16:34,440 But yeah. 185 00:16:34,820 --> 00:16:36,780 So we tried all of this. 186 00:16:36,920 --> 00:16:40,260 We made our RUB chain and we got access to these instructions. 187 00:16:40,820 --> 00:16:44,940 And we found this repo called Custom Processing Unit. 188 00:16:45,540 --> 00:16:49,920 It's an assembler that someone wrote for assembling microcode. 189 00:16:50,080 --> 00:16:58,320 It works from EFSBS, so you can take and compile microcode and put it on a flash drive. 190 00:16:58,560 --> 00:17:01,300 And from there you can apply the patches to your CPU. 191 00:17:01,760 --> 00:17:08,100 And we did that and was hoping that it worked, but as soon as we booted up our Linux system 192 00:17:08,100 --> 00:17:12,060 that we're running on, all of our microcode patches, they were gone. 193 00:17:12,460 --> 00:17:14,680 And what our initial thought was like. 194 00:17:14,940 --> 00:17:16,180 So we can't read their code. 195 00:17:16,800 --> 00:17:18,060 We don't understand it. 196 00:17:18,100 --> 00:17:18,880 It must be wrong. 197 00:17:19,020 --> 00:17:20,120 It can't be anything else. 198 00:17:20,680 --> 00:17:23,500 So of course we started writing our own assembler. 199 00:17:23,940 --> 00:17:25,800 And we turned out to be very wrong. 200 00:17:26,100 --> 00:17:28,980 And the code was running perfectly. 201 00:17:29,420 --> 00:17:33,160 But doing this we took a kind of different approach. 202 00:17:33,760 --> 00:17:38,600 So what you have seen so far has been their syntax, the example we had before. 203 00:17:39,080 --> 00:17:42,260 But we made a more dynamic library. 204 00:17:42,480 --> 00:17:44,920 So actually we made a library. 205 00:17:44,940 --> 00:17:50,040 A Linux dynamically shared library that you can put into any C project that can change 206 00:17:50,040 --> 00:17:52,200 microcode on the fly. 207 00:17:52,620 --> 00:17:57,840 So the code, the assembly code looks a bit more like this. 208 00:17:58,040 --> 00:18:03,500 So we have more dynamic tooling, but also at the cost of syntax. 209 00:18:03,960 --> 00:18:06,660 So what we see here is our function, it's YOLO. 210 00:18:06,880 --> 00:18:10,580 And the first value or the first array here is Ucode patch. 211 00:18:10,820 --> 00:18:12,520 That is the one we are going to apply. 212 00:18:12,840 --> 00:18:14,360 It moves 1337. 213 00:18:14,360 --> 00:18:15,660 Into one of the temp registers. 214 00:18:16,060 --> 00:18:18,620 Then it do dead beef into RBX. 215 00:18:19,060 --> 00:18:22,400 And it takes temp zero and moves into RAX. 216 00:18:22,560 --> 00:18:24,600 So nothing crazy going on here. 217 00:18:24,860 --> 00:18:26,420 And then it ends the instruction. 218 00:18:26,980 --> 00:18:33,160 Then we use the patchUcode function to put these instructions in the RAM area. 219 00:18:33,360 --> 00:18:38,800 And after that we will hook the SUS exit instruction. 220 00:18:39,560 --> 00:18:42,460 And we use that one because it's kind of a nice playground instruction. 221 00:18:44,360 --> 00:18:50,420 It's never really used by Linux kernel or anyone because it's an Intel only instruction. 222 00:18:50,680 --> 00:18:52,040 So it doesn't work on any AMD. 223 00:18:52,720 --> 00:18:57,860 And that is nice because when you fetch a Debian or Ubuntu or whatever, they will work 224 00:18:57,860 --> 00:18:59,140 on both hardwares. 225 00:18:59,280 --> 00:19:03,300 So this instruction is a privileged instruction, never really used. 226 00:19:03,360 --> 00:19:05,680 So we'll snag that one and use it for our purpose. 227 00:19:05,800 --> 00:19:07,520 And we purpose it as we like. 228 00:19:09,040 --> 00:19:09,520 Yeah. 229 00:19:09,780 --> 00:19:10,800 So we did that. 230 00:19:10,900 --> 00:19:14,200 And we started to play around with the microcode. 231 00:19:14,240 --> 00:19:14,340 Yeah. 232 00:19:14,360 --> 00:19:20,860 And one thing we could do with this more dynamic approach, and because we do it from 233 00:19:20,860 --> 00:19:28,280 the C language of sort four, so we also have the power of all of C. And it's dynamic powers 234 00:19:28,280 --> 00:19:29,520 in that we can program. 235 00:19:30,580 --> 00:19:36,720 From C we can program microcode changes versus putting it on a flash drive and being more 236 00:19:36,720 --> 00:19:37,020 static. 237 00:19:37,980 --> 00:19:39,500 So we did that. 238 00:19:39,660 --> 00:19:41,560 And we start just putting in. 239 00:19:42,460 --> 00:19:43,860 So we made microcode. 240 00:19:44,360 --> 00:19:49,980 That traced where is the current instruction pointer from macrocode and where is the current 241 00:19:49,980 --> 00:19:51,440 microcode instruction pointer. 242 00:19:51,660 --> 00:19:55,100 Then we stored that to the normal RAM address space. 243 00:19:55,360 --> 00:19:56,560 And we kept doing that. 244 00:19:56,800 --> 00:20:03,140 And at some point when we lost our microcode changes, we know exactly at what instruction 245 00:20:03,720 --> 00:20:05,220 we were losing these changes. 246 00:20:05,540 --> 00:20:08,200 So we could backtrace, how can we fix that? 247 00:20:08,440 --> 00:20:14,080 And then we discovered that in instruction, let's say instruction that's reading from 248 00:20:14,080 --> 00:20:19,540 the port IO space, so it's reading from hardware, we found that that has a lot of 249 00:20:19,540 --> 00:20:19,980 side effects. 250 00:20:20,300 --> 00:20:25,880 And when we took a look at the opposite one, the write instruction, it was like three lines, 251 00:20:26,080 --> 00:20:27,180 three instructions long. 252 00:20:27,380 --> 00:20:29,260 This one was a couple of hundred long. 253 00:20:29,500 --> 00:20:32,660 It was way bigger than it should be. 254 00:20:33,060 --> 00:20:39,340 So it turns out that doing execution of the in instruction, they have a hidden side effect 255 00:20:40,100 --> 00:20:44,000 from inside, where they check the state. 256 00:20:44,000 --> 00:20:50,600 The micro architectural state and reapplies microcode patches if something seems odd. 257 00:20:51,160 --> 00:20:53,100 That's at least our interpretation of this. 258 00:20:53,460 --> 00:21:00,160 And what we did was that at first we just tried to nuke out the entire instruction and 259 00:21:00,160 --> 00:21:04,240 see if we just like make it a big knob instruction and see if that would work. 260 00:21:04,340 --> 00:21:09,880 Well it did work and we started keeping our microcode changes, but apparently the kernel 261 00:21:09,880 --> 00:21:10,860 needs this instruction. 262 00:21:11,100 --> 00:21:12,540 Now our kernel didn't work. 263 00:21:12,540 --> 00:21:16,180 But there's a solution for that. 264 00:21:16,280 --> 00:21:21,560 We just go 10 instructions down, try and disable it from there, no working. 265 00:21:21,840 --> 00:21:24,640 Okay, what about five instructions longer down? 266 00:21:24,880 --> 00:21:30,920 And yeah, sure enough, when we chopped off that half of the instruction, we started to 267 00:21:30,920 --> 00:21:32,300 getting stable microcode changes. 268 00:21:32,660 --> 00:21:38,220 So now we can update microcode from a Linux user space environment instead of just through 269 00:21:38,220 --> 00:21:38,860 a official. 270 00:21:39,440 --> 00:21:41,880 So yeah, that's nice. 271 00:21:42,580 --> 00:21:44,840 So I think we're going to do a demo of this. 272 00:21:47,940 --> 00:21:48,380 Yeah. 273 00:21:48,520 --> 00:21:54,840 So for the first demo, we're going to show this YOLO function, which we had shown before. 274 00:21:56,240 --> 00:22:01,500 So we're running this on the op squared board, which is already read unlocked and exploited. 275 00:22:02,300 --> 00:22:05,420 So it's just ready to run. 276 00:22:06,100 --> 00:22:10,020 If we look at this code again, let me just... 277 00:22:10,020 --> 00:22:10,120 Okay. 278 00:22:11,800 --> 00:22:12,240 Okay. 279 00:22:12,540 --> 00:22:13,340 So this was the YOLO function. 280 00:22:14,360 --> 00:22:18,660 It sets OX lead in RAX and OX dead in RBX. 281 00:22:18,940 --> 00:22:23,400 And then we have this wrapper around it, which has some inline assembly and that just calls 282 00:22:23,400 --> 00:22:24,460 the syssq instruction. 283 00:22:26,760 --> 00:22:33,880 If we then just open it up in GDB. 284 00:22:35,700 --> 00:22:38,780 So now we're right before the YOLO function. 285 00:22:39,020 --> 00:22:40,300 And if we step over it. 286 00:22:40,500 --> 00:22:40,940 Now. 287 00:22:40,940 --> 00:22:42,860 Now our patch should be applied. 288 00:22:44,340 --> 00:22:47,020 And now we're right at the sys exit instruction. 289 00:22:47,380 --> 00:22:52,320 So if we step now, what should happen, normally at least, is we should get a general protection 290 00:22:52,320 --> 00:22:56,960 fault because this is a privilege instruction and we're in user space. 291 00:22:57,380 --> 00:23:04,340 But what actually happens is that if you look over the top, RAX is set to OX lead and RBX 292 00:23:04,340 --> 00:23:05,220 is set to OX dead. 293 00:23:05,620 --> 00:23:08,900 So our patch was successfully added. 294 00:23:09,060 --> 00:23:09,340 Okay. 295 00:23:09,340 --> 00:23:18,280 And now for probably the demonstration you've all been waiting for, the back door. 296 00:23:20,840 --> 00:23:24,980 But to show it, we are going to open up a browser. 297 00:23:26,480 --> 00:23:37,740 And here we have just a site hosted here with a sweet little link here. 298 00:23:37,860 --> 00:23:39,060 Click to get a calc. 299 00:23:39,340 --> 00:23:39,940 Let's try and press it. 300 00:23:40,780 --> 00:23:41,800 And a calc pops. 301 00:23:50,660 --> 00:23:52,140 So that's very neat. 302 00:23:52,380 --> 00:23:56,480 But our exploit actually doesn't have anything to do with the JavaScript engine. 303 00:23:56,960 --> 00:23:58,080 So wait. 304 00:23:58,420 --> 00:24:00,320 Maybe if we open up Firefox. 305 00:24:05,340 --> 00:24:05,880 Maybe. 306 00:24:07,220 --> 00:24:07,760 Okay. 307 00:24:08,720 --> 00:24:09,260 Okay. 308 00:24:10,220 --> 00:24:11,700 We will get another calc. 309 00:24:17,060 --> 00:24:20,180 But we wanted something even more general than that. 310 00:24:20,360 --> 00:24:26,840 So if we actually just copy the image link here and pop it into our shell and just W 311 00:24:26,840 --> 00:24:30,700 get it, another calc pops. 312 00:24:37,180 --> 00:24:39,320 So apparently, just to show you how it works, we're going to get a calc here. 313 00:24:39,320 --> 00:24:41,780 And just getting this image pops a calc. 314 00:24:41,820 --> 00:24:44,540 So how the hell does that even work? 315 00:24:49,560 --> 00:24:50,120 Okay. 316 00:24:50,400 --> 00:24:50,960 Thanks. 317 00:24:51,220 --> 00:24:51,720 Yeah. 318 00:24:51,860 --> 00:24:53,880 So you're probably wondering how this works. 319 00:24:54,360 --> 00:25:01,560 So let's so for this, when we were engineering this, we were thinking about, okay, what kind 320 00:25:01,560 --> 00:25:03,360 of instructions can we target? 321 00:25:03,680 --> 00:25:08,640 Where is the place that we should put this back door? 322 00:25:08,640 --> 00:25:11,240 So the RAM is very limited. 323 00:25:11,400 --> 00:25:12,240 We have few spaces. 324 00:25:12,660 --> 00:25:15,140 And we needed a good instruction. 325 00:25:15,560 --> 00:25:17,040 What makes up a good instruction? 326 00:25:17,280 --> 00:25:20,080 Well, it needs to be run very frequently. 327 00:25:20,280 --> 00:25:22,500 So every program should be using this instruction. 328 00:25:23,320 --> 00:25:29,760 Furthermore, this instruction that we are hooking into needs to touch some kind of user 329 00:25:29,760 --> 00:25:36,700 control data because, like, we can't store the entire back door inside of this tiny micro 330 00:25:36,700 --> 00:25:37,180 code space. 331 00:25:37,180 --> 00:25:41,360 So we need something that touches user, that has user input. 332 00:25:41,840 --> 00:25:45,340 So our attention was drawn to this syscall instruction. 333 00:25:45,760 --> 00:25:50,820 The syscall instruction is the one in charge of handling kernel requests. 334 00:25:51,120 --> 00:25:58,480 So when you make a syscall, you, in the RAX register, you put a number specifying 335 00:25:58,920 --> 00:26:01,180 what you would like the kernel to do. 336 00:26:01,740 --> 00:26:06,920 And in the RCX register, the CPU will put the return. 337 00:26:06,920 --> 00:26:12,980 So where should the kernel return to after the syscall has been handled? 338 00:26:13,140 --> 00:26:18,560 If it's successful, then we should send it back to user space at the address specified 339 00:26:18,560 --> 00:26:19,400 at RCX. 340 00:26:19,760 --> 00:26:25,960 And also, a nice feature is that a lot of syscalls take, like, parameters. 341 00:26:26,320 --> 00:26:30,820 And some of those can be that user data that we're looking for. 342 00:26:31,240 --> 00:26:33,520 So let's jump into some sort of code. 343 00:26:33,740 --> 00:26:36,480 We can't show the entire micro code changes. 344 00:26:36,920 --> 00:26:38,480 Because it's very, very big. 345 00:26:38,600 --> 00:26:44,940 So we'll do a sort of C thing that will tell how this backdoor works. 346 00:26:45,240 --> 00:26:50,920 So we hook the syscall instruction right in the beginning of the syscall instruction. 347 00:26:51,440 --> 00:26:57,180 And then we first check, is this, is RAX says to a syscall write? 348 00:26:57,540 --> 00:27:03,380 Because if it's a write instruction, then it either writes to, like, standard out or 349 00:27:03,380 --> 00:27:05,480 it's doing file manipulation or stuff like that. 350 00:27:05,480 --> 00:27:12,080 So, for example, if Chrome or Firefox is going to cache an image you need for later, 351 00:27:12,320 --> 00:27:15,020 then it will do a write to the file system. 352 00:27:15,600 --> 00:27:16,940 So that's pretty neat. 353 00:27:17,140 --> 00:27:21,460 Then the next check we make here is that we just check for a magic value. 354 00:27:21,720 --> 00:27:25,460 We don't want to execute every image pulled from the internet. 355 00:27:26,940 --> 00:27:31,640 And if one of these cases fail, go do the normal syscall. 356 00:27:32,000 --> 00:27:34,140 But if we hit both of these conditions. 357 00:27:34,360 --> 00:27:35,460 . . . . . . 358 00:27:35,460 --> 00:27:35,480 . . . . . 359 00:27:35,480 --> 00:27:39,320 And the first thing we'll do is we will save the current state of the CPU. 360 00:27:39,740 --> 00:27:45,080 So we'll take all of the registers that we touched during our actual shulker, and 361 00:27:45,080 --> 00:27:46,260 we will store it into memory. 362 00:27:47,000 --> 00:27:48,680 That's just what we're doing here. 363 00:27:48,800 --> 00:27:51,760 We'll save the state of the CPU so we can execute it. 364 00:27:52,480 --> 00:27:58,560 After that, we set RCX, and we put it at RSI. 365 00:27:58,900 --> 00:28:05,440 And the reason we take RSI is because is the RSI register contains the instance advertisement. 366 00:28:05,460 --> 00:28:11,240 address for the image data so that is the image data itself and then we add a 367 00:28:11,240 --> 00:28:17,120 small offset and then we will now say okay colonel when you're done doing your 368 00:28:17,120 --> 00:28:23,440 request now this is a new address that you should return back to after handling 369 00:28:23,440 --> 00:28:29,580 whatever you're going to do so when you return to user space execute actually 370 00:28:29,580 --> 00:28:33,500 our image data but now you may say oh wait hold on a minute 371 00:28:33,500 --> 00:28:40,060 CPUs nowadays has memory has MMUs memory management units so you can't 372 00:28:40,060 --> 00:28:46,440 just execute arbitrary data everywhere you like that is a neat trick about the 373 00:28:46,440 --> 00:28:52,960 syscall instruction because Linux has a syscall called mprotect so instead of 374 00:28:52,960 --> 00:29:00,000 saving the image and doing the write syscall well let's put a syscall this 375 00:29:00,000 --> 00:29:03,100 is mprotect into our X and change the 376 00:29:03,100 --> 00:29:03,480 request to the image data and then you can see that the image data is now 377 00:29:03,480 --> 00:29:06,140 in the image itself so let's do a quick test we make to the colonel and say to 378 00:29:06,140 --> 00:29:12,620 the colonel hey this address space in RSI can you please go and make that read 379 00:29:12,620 --> 00:29:18,840 write and executable at the same time and then when the colonel jumps out of 380 00:29:18,840 --> 00:29:24,800 the colonel space it will jump happily into executable code that we control 381 00:29:24,800 --> 00:29:30,020 that is our image oh well it's stored in the metadata of the image so you don't 382 00:29:30,020 --> 00:29:37,100 see anything on the image itself. So let's briefly go over what 383 00:29:37,800 --> 00:29:42,360 shellcode we actually put in. It's super simple. The first thing we do is do 384 00:29:42,360 --> 00:29:48,560 a fork and then we have two processes running parallel. The parent process will 385 00:29:48,560 --> 00:29:54,240 now be the new main thread actually handling the browser 386 00:29:54,240 --> 00:30:00,500 like Chrome stuff. So what we do is we restore the saved context from the ones 387 00:30:00,500 --> 00:30:05,160 we saved in memory. The step we did where we stored all the registers 388 00:30:05,160 --> 00:30:09,740 into the image states itself, we'll now pull that back out and put it back 389 00:30:09,740 --> 00:30:14,260 in place in the CPU in that thread. Then Chrome can continue its execution 390 00:30:15,060 --> 00:30:19,800 happy and won't notice that we have spawned a child thread. Now the child 391 00:30:19,800 --> 00:30:24,020 thread, they can just go do all the evil stuff it wants. In this case 392 00:30:24,020 --> 00:30:24,220 it's going to do all the evil stuff it wants. In this case it's going to do all the evil stuff it wants. In this case it's going to do all the evil stuff it wants. In this case 393 00:30:24,220 --> 00:30:28,200 it's going to do all the evil stuff it wants. In this case it's going to do all the evil stuff it wants. In this case it's popping a calculator. So before we 394 00:30:28,200 --> 00:30:33,340 finalize let's look a bit at how did we do some of this reversion process. Of 395 00:30:33,340 --> 00:30:36,680 course there has been some pioneers in the field that started out this 396 00:30:36,680 --> 00:30:44,020 positive technology and custom processing unit etc. But still a lot of 397 00:30:44,020 --> 00:30:51,720 unknown instructions in there. So how do you reverse instructions in a new 398 00:30:51,720 --> 00:30:53,980 microcode assembly? 399 00:30:53,980 --> 00:30:59,400 Simply language where half of the opcodes is just doing unknown stuff. 400 00:30:59,940 --> 00:31:05,140 One trick we did is that we used a lot of what we call side-by-side reversing. 401 00:31:05,780 --> 00:31:11,260 So on one half of the screen we take the Soto code for this instruction and on 402 00:31:11,260 --> 00:31:15,480 the other half we will have the actual implementation and then we can go back 403 00:31:15,480 --> 00:31:20,380 and forth and see oh on this side we have three compares there or like three 404 00:31:20,380 --> 00:31:23,640 writes or like looking for bit patterns in the Soto code. 405 00:31:23,640 --> 00:31:27,620 And seeing oh we have the same three writes over in the actual implementation 406 00:31:27,620 --> 00:31:33,780 and that and just after that we have that unknown thing the unknown opcode. 407 00:31:33,940 --> 00:31:39,120 But what is it in the Soto code and can we extract info from that? That is one 408 00:31:39,120 --> 00:31:43,460 thing. Another is just like tracing like the dynamic tracing we saw before 409 00:31:43,460 --> 00:31:48,920 holding in the middle of instructions and pulling out the data. That could be 410 00:31:48,920 --> 00:31:52,220 like from GDB that you saw before. And also 411 00:31:53,820 --> 00:32:00,340 yeah just maybe copy pasting stuff. Like if we have a big instruction then 412 00:32:00,340 --> 00:32:05,640 copy paste the whole thing into the RAM address space and just redirect it. Now 413 00:32:05,640 --> 00:32:09,380 you have a clone and now you can start flipping one bit at a time and seeing 414 00:32:09,380 --> 00:32:14,700 what slight changes did that affect. Most of the time it's a CPU crash bit but 415 00:32:14,700 --> 00:32:19,000 some of the times we do get laggy and we see different values in our register 416 00:32:19,720 --> 00:32:20,900 after that instruction. 417 00:32:22,380 --> 00:32:23,620 Yeah we talked a bit about that. 418 00:32:23,640 --> 00:32:28,060 We talked a bit about playground instructions like SUSEXIT because it's never used but 419 00:32:28,060 --> 00:32:33,360 there's actually more of these. So some of the virtualization instructions for 420 00:32:33,360 --> 00:32:38,800 virtual machines, hypervisation and stuff like that. That could be VM ride, VM 421 00:32:38,800 --> 00:32:44,600 enter and some of the hypervisation. Those are instructions 422 00:32:44,600 --> 00:32:49,960 that is also never used unless you are doing virtual machines. But if you don't 423 00:32:49,960 --> 00:32:53,440 do that these instructions will never be used. And some of those things 424 00:32:53,640 --> 00:32:58,580 are like arguments in form of registers. So there you get playground instructions 425 00:32:59,140 --> 00:33:02,380 which also happens to decode registers. 426 00:33:04,020 --> 00:33:09,860 So and finally before heading off we want to talk about future work that we or even 427 00:33:09,860 --> 00:33:14,380 you might do because we are uploading everything right after this and making everything public. 428 00:33:15,000 --> 00:33:21,900 So we really wanted to find CPU bugs or should we say exploit CPU bugs. Because we actually 429 00:33:21,900 --> 00:33:23,560 get a little bit of help from Intel here. 430 00:33:23,640 --> 00:33:30,060 If we do apply our microcode patches, the official ones from Intel, then we can also 431 00:33:30,060 --> 00:33:37,140 look inside the matching patch registers that we saw before. Now we know where do Intel 432 00:33:37,740 --> 00:33:44,260 like hook from and to in the ROM and RAM address space. So now we can have a look inside 433 00:33:44,260 --> 00:33:51,980 what bugs have they already discovered and patched that we have never seen. And our goal 434 00:33:51,980 --> 00:33:52,780 or hope 435 00:33:52,780 --> 00:34:00,200 is that we could re-unlock the CPU. So put it into this Intel debug mode without having 436 00:34:00,200 --> 00:34:04,780 the physical access beforehand. If we can abuse some of these bugs. We think that it 437 00:34:04,780 --> 00:34:11,620 could be possible that one could take control of the micro instruction pointer and point 438 00:34:11,620 --> 00:34:18,220 it to some of these debug instructions. That would be a very cool thing to do and hopefully 439 00:34:18,220 --> 00:34:19,420 we get the time to do it. 440 00:34:20,600 --> 00:34:22,380 Now last I want to say 441 00:34:22,780 --> 00:34:27,800 thank you to especially to Calmer Union. They have been, that's our CTF team. They have been 442 00:34:27,800 --> 00:34:33,320 a great help both sponsoring but also having a hundred plus hackers around. Always available 443 00:34:33,920 --> 00:34:39,260 to answer any stupid questions you might have about a CPU. Like really thank you guys. 444 00:34:39,480 --> 00:34:45,260 And also really we acknowledge the work that Mark Elno, Positive Technology and Trust and 445 00:34:45,260 --> 00:34:50,000 Processing Unit has done to lay the ground for this talk. Thank you for listening in 446 00:34:50,640 --> 00:34:52,600 and do you all have some questions? 447 00:34:52,640 --> 00:34:52,760 Subtitles by the Amara.org community 448 00:35:09,360 --> 00:35:10,400 Yeah, that's one. 449 00:35:12,960 --> 00:35:16,600 The first question is how do we know that we can do this? 450 00:35:16,680 --> 00:35:19,780 What are the features that you have to do this? 451 00:35:20,480 --> 00:35:22,520 What are the features that you have to do this? 452 00:35:26,060 --> 00:35:31,000 So the question is what are the primitives, could you imagine building from this? 453 00:35:31,220 --> 00:35:35,920 So in general I think this is a very strong primitive, like putting a backdoor so hidden away. 454 00:35:36,220 --> 00:35:40,100 It's basically completely undetectable because the OS can't see it. 455 00:35:41,580 --> 00:35:46,080 But I could imagine like for example game hacking and stuff like that. 456 00:35:46,200 --> 00:35:52,500 Like you can hide it away from any like a cheat engine check or stuff like that. 457 00:35:52,500 --> 00:35:54,500 I could imagine that could be useful there. 458 00:35:54,760 --> 00:35:56,440 Other than that, I don't know. 459 00:35:56,560 --> 00:35:57,120 I don't know. 460 00:36:03,580 --> 00:36:04,020 Okay. 461 00:36:04,560 --> 00:36:05,340 Yeah, that's one. 462 00:36:05,920 --> 00:36:07,660 Where can you buy the dev boards? 463 00:36:07,820 --> 00:36:10,320 Where can you buy the dev boards? 464 00:36:10,460 --> 00:36:16,780 Okay, the dev board is a up squared and we on the slide you will find a link to our 465 00:36:16,780 --> 00:36:17,380 documentation. 466 00:36:17,900 --> 00:36:21,900 And on there it will have the exact dev board that we use. 467 00:36:22,380 --> 00:36:29,160 You can order it online and we have a flash image that you can just flash on it and 468 00:36:29,160 --> 00:36:30,780 then you're ready to go to play with this. 469 00:36:34,580 --> 00:36:35,800 Okay, I think there's no more. 470 00:36:35,940 --> 00:36:36,680 Thank you for listening.