Hi and welcome to our talk Backdoor in the Core. My name is Alexander Krogh and I'm a vulnerability researcher at Vectorize. Besides that I'm also a CTF player and through that community that's where I learned this wonderful guy. We've been playing together for a few years now and yeah this is our talk about microcode. He's a grad student at the Danish Technical University and it's about microcode and hacking CPUs. But first before we dig into the microcode itself need a little crash course on computer architecture. So what we see up here is a CPU. It's a CPU core from the Skylake processors. Inside of it we have both the front end that's the up top and we have the back end down beneath it and some memory. Subsystem in the bottom. The front end is mostly in charge of doing stuff like fetching new instructions so that's where your your high level saying high level for instructions here that's the x86 instructions going in there into the fetch queue and they are getting decoded and then they will be translated into micro operations and micro operations is what's actually running on the CPU. And micro operations are then scheduled by the back end. The back end is the ALU so the arithmetic logic unit and floating point unit and stuff like that and through that the resulting stuff can go to memory for the memory subsystem. We are only going to take a very tiny section of this CPU and they dive into it today. So the one we are diving into is this little tiny section it's instruction queue and the decoding phase. So here when a instruction comes in and it goes through the queue and if it's a very simple instruction like just a simple move or addition XOR something simple like that it will go straight through the simple decoders and they have a one-to-one sort of mapping between micro operations and the high level macro operations. So we call it macro operations and micro operations from the ones going deep in the CPU. So more complex instructions will go through the complex decoder and what we're looking at today is the really heavy instructions. So that's something like return from a system call or like when you enter a hypervised space. Some of these instructions are multiple hundreds of lines long or multiple hundred micro operations long. So they can't just be directly decoded into our micro operations. They go to the ROM and on the ROM we fetch yeah we have like kind of like our normal CPU. We have micro codes so you have an instruction pointer running through an executing code and today we're looking at that space. So let's look so yeah so what why do we need this? Why do we need this complex set of micro code? Well first of all we have the complex instructions. Those cannot be directly implemented transistors but also bugs do occur in CPUs. So we we had like a Spectrum meltdown a few years back setting the scene of all of this and just a few weeks ago. We saw the simply bug. So more and more of these kind of CPU bugs are CPU bugs are popping up here and there and to fix these bugs post-production we need micro code updates. So besides the ROM area where micro code is stored we also have a small RAM address space and that is writable and that's what we're going to look at today. Normally these updates are assigned and encrypted by Intel so you can't really have that deep dive and go look what's actually pushed by Intel to the CPUs. There hasn't really been a way so far to like inspect that and we'll see what kind of damage we can do with that if we actually get the access. So this is a table showing how the address space is laid out inside of these CPUs. It's a very simple address space. It's totally linear. So it goes from zero down to a 0x800 or 8000 sorry and somewhere in the middle we see a split going from read-only at 7c00. That's where the writable address space starts. So that's where we can inject micro code and out on the columns we see the instructions themselves. They are we have grouped them together in this table because that's how they're grouped in the CPU. We call they're called triads. So I'm going to show you a one triad is three instructions and one implicit knob instruction so every fourth instructions would just be an implicit knob you can't address them you can't jump to them but they won't really execute anything besides that we have the last thing and that's a secret sequence word it's stored separate separately from the instructions and the second word is what groups these uh tries together they are the ones in charge of doing um flow controls so they can like do branching based on testings test instructions and stuff like that uh so let's take the first example of how um a instruction could be implemented in microcode this is the x adder instruction it's exchange and addition in the same time no normally we see these x86 instructions as a one atomic thing but they're actually not so this x addition is supposed of these three micro operations um so let's read through it so the first thing happening is that we take uh the source register in this case on the first line and or it with a zero the source register is the rbx um in this case because it's on that side and rax is the destination register so oring with a zero that just means like that's basically just a move we move it into a zero is a physical and temporary register inside the cpu that you normally can't access um yeah so that's a simple move then we do a syracuse zero extent uh but in this case it's 64 bits so it's basically just a again just a move so we move it into the uh r64 source register so that's basically taking rax and putting into rdx um so that's the exchange part of this instruction after that we do the final step and that's the addition so we take the safe temporary register and add it back into the destination register and we also store the resulting value in the destination register and now at last we see the final part that's the sequence word coming into play and in this case it's a simple u end and that means that the instruction the macro instruction the x add will end at this point so we will go and update uh the instruction pointer and fetch a new one now simple example so what you saw before what's the temp 0 register that's a new one to most of us that's a temporary register only used inside microcode and it's not visible to um yeah to the macro code and we got 16 of these register 0 through 15 they called um of course my microcode can also access all of the normal register registers, like RX, RBX, and stuff like that. Besides that, we got eight floating-point registers, just same thing as the temp registers, there's eight of them for XMM instructions, also temporarily. Then we can of course access system registers, that can be like segmentation of memory, some of those registers, they're the ones that holds the state. And last we can, we'll hold what modes the CPUs are running in, and all the critical stuff, so like are we in 32-bit mode, are we in 64-bit mode, are we in a hyper-vast space, and stuff like that. And often the CPU will check that flag, and do conditional jumps in microcode based on what state are we in, are we in privilege mode, are we in kernel space, stuff like that. Of course, from microcode we can also access memory. There's multiple ways of doing this, one is for example, we can access memory, we can access memory, we can access memory, for example, the virtual address space, the ones we usually do from macro operations, so we can store and read memory, just as that. We can also access directly from this physical address space, going just around all the page tables and stuff like that. So, yeah. And besides that, we have some very, very tiny memory, dedicated for only microcode and inside the CPU. That's the U-code memory. It's a separate address space from where we are storing the actual instructions. So, data and instructions are separate completely, but here we have around 0x100 keywords that we can also use as temporary storage. Each entry is 64-bit wide, and it is used, the difference between those and temporary instructions is that these can be used across multiple macro instructions. So, temp registers is for temporary storage inside one macro instructions, and this can store safe control registers when switching mode and stuff like that, and then we can fetch it out later. So, each entry is like, has a dedicated purpose. Other than that, we also have two buses, or a bus and a fabric. These are used for communication within the CPU. One of the very interesting ones is the one that we have here, which is the one that we call the control register bus. It can talk to all components inside one single CPU core. That could be the caches that are on the core. So, for example, an L1 cache, maybe an L2, something like that, and besides that, we have the very important one, the microcode sequencer. That is the one in charge of scheduling instructions and going through the pipelines. So, if we can go to the control register bus, we can access the microcode as well. Other than that, we have the insole-on system fabric. That is mostly used for external communication. So, the HTT driver and USB stuff and other components that are shared between some of the cores. So, that is mostly for external communication outside of the main core. Yes? Okay. So, now we talked a bit about that. How do we place these updates in microcode? So, we talked about that it's a ROM area, and it's also a memory area. So, we talked about that it's a ROM area, and that is read-only. So, when Intel pushes an update, how do they actually update a ROM area? That's where these match and patch registers come into place. What we see here is the bit fields and how a match and patch register is laid out. And we have 32 of these, and each time with a new instruction, we will check these fields, and if we get a hit, we will jump to that address instead. So, we have the present bit. That will tell if this match and patch is enabled then we have a source field that is if we hit this address go jump somewhere else and that will be what's stored in the destination and as you see up here both source and destination are to you at us so they are microcode addresses but they shifted by one so one lose one bit of position and we'll talk a bit about how that works so so here we have an example of a matching patch register so let's say our CPU is running and the microcode instruction pointer hits free c8 then we have the matching patch hit because we see a entry in the matching patch table with the same value that means that we will instead go and jump directly to 7c 0 0 and start executing from there instead and this is what Intel programs into the CPU every time we apply a microcode update which happens every on every reboot but we also find out through trial and error that this loss in position means that if we do his free c9 we would actually also get a hit and then we'll jump jump to the red area instead so we will jump one micro instruction further on so how can we use this or should I say abuses when researching CPUs so a lot of things we have been doing is to do dynamic inspection of state just to fake also to figure out what this code doing but also to figure out what's instructions doing so here we have a simple example taken from the ROM it reads first from the RAM address space into temp 3 and temp 2 but what if you want to inspect the state well one thing we could do is that we can tell the matching patch registers to come go and jump somewhere else so we see the the all over crossed line the red line that is the test use date and then normally we would the sequence word will tell it to jump somewhere to the general protection fault and do a fault because this is a privileged instruction but we can change that so we swap that out and say go to the RAM area and in the RAM we have put our stuff and we will flip the test case and when we do that we can tell it to either go and take the left part and say move , move these temporary registers into the interacts and rbx these are registers that we can inspect from from a debugger and from normal user space or if we are in a privileged mode we don't want to screw up this instruction so instead we send it to its normal path and send it back the way we came from so we don't mess up the instruction and the CPU will still continue to function and run happily ever after okay so how do we manage to make these change now how can we access the micro code sequencer when it's all locked down and locked up by Incil well luckily the people at positive technology they found a vulnerability in the Incil management engine that runs a software called trusted execution unit and they found a buffer overflow and that engine runs on a very highly privileged level and through that we can enable debug features Features. Debug features only, Intel is supposed to have access to, and that unlocks hidden instructions and undocumented instructions. So yeah, we took the POC and expanded it to these dev boards. That's actually the board lying up here that we are presenting from. And we have, of course, prepared flash images and stuff that you can flash on, so if you buy one of these, you can go and replicate some of our findings. The two instructions that we mostly use is the uDebugRead and the uDebugWrite. These two instructions can write to microcode, it can read and write to the data areas we talked before. The Write instruction also has a special feature, where you can put in an address in the RACS register and just tell it to go jump straight. So an address you specify in the microcode area. And it can also talk to the control register bus that we talked about before. So that's how we access all of these microcode features. Really good findings over at the post-exam early side. But yeah. So we tried all of this. We made our RUB chain and we got access to these instructions. And we found this repo called Custom Processing Unit. It's an assembler that someone wrote for assembling microcode. It works from EFSBS, so you can take and compile microcode and put it on a flash drive. And from there you can apply the patches to your CPU. And we did that and was hoping that it worked, but as soon as we booted up our Linux system that we're running on, all of our microcode patches, they were gone. And what our initial thought was like. So we can't read their code. We don't understand it. It must be wrong. It can't be anything else. So of course we started writing our own assembler. And we turned out to be very wrong. And the code was running perfectly. But doing this we took a kind of different approach. So what you have seen so far has been their syntax, the example we had before. But we made a more dynamic library. So actually we made a library. A Linux dynamically shared library that you can put into any C project that can change microcode on the fly. So the code, the assembly code looks a bit more like this. So we have more dynamic tooling, but also at the cost of syntax. So what we see here is our function, it's YOLO. And the first value or the first array here is Ucode patch. That is the one we are going to apply. It moves 1337. Into one of the temp registers. Then it do dead beef into RBX. And it takes temp zero and moves into RAX. So nothing crazy going on here. And then it ends the instruction. Then we use the patchUcode function to put these instructions in the RAM area. And after that we will hook the SUS exit instruction. And we use that one because it's kind of a nice playground instruction. It's never really used by Linux kernel or anyone because it's an Intel only instruction. So it doesn't work on any AMD. And that is nice because when you fetch a Debian or Ubuntu or whatever, they will work on both hardwares. So this instruction is a privileged instruction, never really used. So we'll snag that one and use it for our purpose. And we purpose it as we like. Yeah. So we did that. And we started to play around with the microcode. Yeah. And one thing we could do with this more dynamic approach, and because we do it from the C language of sort four, so we also have the power of all of C. And it's dynamic powers in that we can program. From C we can program microcode changes versus putting it on a flash drive and being more static. So we did that. And we start just putting in. So we made microcode. That traced where is the current instruction pointer from macrocode and where is the current microcode instruction pointer. Then we stored that to the normal RAM address space. And we kept doing that. And at some point when we lost our microcode changes, we know exactly at what instruction we were losing these changes. So we could backtrace, how can we fix that? And then we discovered that in instruction, let's say instruction that's reading from the port IO space, so it's reading from hardware, we found that that has a lot of side effects. And when we took a look at the opposite one, the write instruction, it was like three lines, three instructions long. This one was a couple of hundred long. It was way bigger than it should be. So it turns out that doing execution of the in instruction, they have a hidden side effect from inside, where they check the state. The micro architectural state and reapplies microcode patches if something seems odd. That's at least our interpretation of this. And what we did was that at first we just tried to nuke out the entire instruction and see if we just like make it a big knob instruction and see if that would work. Well it did work and we started keeping our microcode changes, but apparently the kernel needs this instruction. Now our kernel didn't work. But there's a solution for that. We just go 10 instructions down, try and disable it from there, no working. Okay, what about five instructions longer down? And yeah, sure enough, when we chopped off that half of the instruction, we started to getting stable microcode changes. So now we can update microcode from a Linux user space environment instead of just through a official. So yeah, that's nice. So I think we're going to do a demo of this. Yeah. So for the first demo, we're going to show this YOLO function, which we had shown before. So we're running this on the op squared board, which is already read unlocked and exploited. So it's just ready to run. If we look at this code again, let me just... Okay. Okay. So this was the YOLO function. It sets OX lead in RAX and OX dead in RBX. And then we have this wrapper around it, which has some inline assembly and that just calls the syssq instruction. If we then just open it up in GDB. So now we're right before the YOLO function. And if we step over it. Now. Now our patch should be applied. And now we're right at the sys exit instruction. So if we step now, what should happen, normally at least, is we should get a general protection fault because this is a privilege instruction and we're in user space. But what actually happens is that if you look over the top, RAX is set to OX lead and RBX is set to OX dead. So our patch was successfully added. Okay. And now for probably the demonstration you've all been waiting for, the back door. But to show it, we are going to open up a browser. And here we have just a site hosted here with a sweet little link here. Click to get a calc. Let's try and press it. And a calc pops. So that's very neat. But our exploit actually doesn't have anything to do with the JavaScript engine. So wait. Maybe if we open up Firefox. Maybe. Okay. Okay. We will get another calc. But we wanted something even more general than that. So if we actually just copy the image link here and pop it into our shell and just W get it, another calc pops. So apparently, just to show you how it works, we're going to get a calc here. And just getting this image pops a calc. So how the hell does that even work? Okay. Thanks. Yeah. So you're probably wondering how this works. So let's so for this, when we were engineering this, we were thinking about, okay, what kind of instructions can we target? Where is the place that we should put this back door? So the RAM is very limited. We have few spaces. And we needed a good instruction. What makes up a good instruction? Well, it needs to be run very frequently. So every program should be using this instruction. Furthermore, this instruction that we are hooking into needs to touch some kind of user control data because, like, we can't store the entire back door inside of this tiny micro code space. So we need something that touches user, that has user input. So our attention was drawn to this syscall instruction. The syscall instruction is the one in charge of handling kernel requests. So when you make a syscall, you, in the RAX register, you put a number specifying what you would like the kernel to do. And in the RCX register, the CPU will put the return. So where should the kernel return to after the syscall has been handled? If it's successful, then we should send it back to user space at the address specified at RCX. And also, a nice feature is that a lot of syscalls take, like, parameters. And some of those can be that user data that we're looking for. So let's jump into some sort of code. We can't show the entire micro code changes. Because it's very, very big. So we'll do a sort of C thing that will tell how this backdoor works. So we hook the syscall instruction right in the beginning of the syscall instruction. And then we first check, is this, is RAX says to a syscall write? Because if it's a write instruction, then it either writes to, like, standard out or it's doing file manipulation or stuff like that. So, for example, if Chrome or Firefox is going to cache an image you need for later, then it will do a write to the file system. So that's pretty neat. Then the next check we make here is that we just check for a magic value. We don't want to execute every image pulled from the internet. And if one of these cases fail, go do the normal syscall. But if we hit both of these conditions. . . . . . . . . . . . And the first thing we'll do is we will save the current state of the CPU. So we'll take all of the registers that we touched during our actual shulker, and we will store it into memory. That's just what we're doing here. We'll save the state of the CPU so we can execute it. After that, we set RCX, and we put it at RSI. And the reason we take RSI is because is the RSI register contains the instance advertisement. address for the image data so that is the image data itself and then we add a small offset and then we will now say okay colonel when you're done doing your request now this is a new address that you should return back to after handling whatever you're going to do so when you return to user space execute actually our image data but now you may say oh wait hold on a minute CPUs nowadays has memory has MMUs memory management units so you can't just execute arbitrary data everywhere you like that is a neat trick about the syscall instruction because Linux has a syscall called mprotect so instead of saving the image and doing the write syscall well let's put a syscall this is mprotect into our X and change the request to the image data and then you can see that the image data is now in the image itself so let's do a quick test we make to the colonel and say to the colonel hey this address space in RSI can you please go and make that read write and executable at the same time and then when the colonel jumps out of the colonel space it will jump happily into executable code that we control that is our image oh well it's stored in the metadata of the image so you don't see anything on the image itself. So let's briefly go over what shellcode we actually put in. It's super simple. The first thing we do is do a fork and then we have two processes running parallel. The parent process will now be the new main thread actually handling the browser like Chrome stuff. So what we do is we restore the saved context from the ones we saved in memory. The step we did where we stored all the registers into the image states itself, we'll now pull that back out and put it back in place in the CPU in that thread. Then Chrome can continue its execution happy and won't notice that we have spawned a child thread. Now the child thread, they can just go 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 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 it's popping a calculator. So before we finalize let's look a bit at how did we do some of this reversion process. Of course there has been some pioneers in the field that started out this positive technology and custom processing unit etc. But still a lot of unknown instructions in there. So how do you reverse instructions in a new microcode assembly? Simply language where half of the opcodes is just doing unknown stuff. One trick we did is that we used a lot of what we call side-by-side reversing. So on one half of the screen we take the Soto code for this instruction and on the other half we will have the actual implementation and then we can go back and forth and see oh on this side we have three compares there or like three writes or like looking for bit patterns in the Soto code. And seeing oh we have the same three writes over in the actual implementation and that and just after that we have that unknown thing the unknown opcode. But what is it in the Soto code and can we extract info from that? That is one thing. Another is just like tracing like the dynamic tracing we saw before holding in the middle of instructions and pulling out the data. That could be like from GDB that you saw before. And also yeah just maybe copy pasting stuff. Like if we have a big instruction then copy paste the whole thing into the RAM address space and just redirect it. Now you have a clone and now you can start flipping one bit at a time and seeing what slight changes did that affect. Most of the time it's a CPU crash bit but some of the times we do get laggy and we see different values in our register after that instruction. Yeah we talked a bit about that. We talked a bit about playground instructions like SUSEXIT because it's never used but there's actually more of these. So some of the virtualization instructions for virtual machines, hypervisation and stuff like that. That could be VM ride, VM enter and some of the hypervisation. Those are instructions that is also never used unless you are doing virtual machines. But if you don't do that these instructions will never be used. And some of those things are like arguments in form of registers. So there you get playground instructions which also happens to decode registers. So and finally before heading off we want to talk about future work that we or even you might do because we are uploading everything right after this and making everything public. So we really wanted to find CPU bugs or should we say exploit CPU bugs. Because we actually get a little bit of help from Intel here. If we do apply our microcode patches, the official ones from Intel, then we can also look inside the matching patch registers that we saw before. Now we know where do Intel like hook from and to in the ROM and RAM address space. So now we can have a look inside what bugs have they already discovered and patched that we have never seen. And our goal or hope is that we could re-unlock the CPU. So put it into this Intel debug mode without having the physical access beforehand. If we can abuse some of these bugs. We think that it could be possible that one could take control of the micro instruction pointer and point it to some of these debug instructions. That would be a very cool thing to do and hopefully we get the time to do it. Now last I want to say thank you to especially to Calmer Union. They have been, that's our CTF team. They have been a great help both sponsoring but also having a hundred plus hackers around. Always available to answer any stupid questions you might have about a CPU. Like really thank you guys. And also really we acknowledge the work that Mark Elno, Positive Technology and Trust and Processing Unit has done to lay the ground for this talk. Thank you for listening in and do you all have some questions? Subtitles by the Amara.org community Yeah, that's one. The first question is how do we know that we can do this? What are the features that you have to do this? What are the features that you have to do this? So the question is what are the primitives, could you imagine building from this? So in general I think this is a very strong primitive, like putting a backdoor so hidden away. It's basically completely undetectable because the OS can't see it. But I could imagine like for example game hacking and stuff like that. Like you can hide it away from any like a cheat engine check or stuff like that. I could imagine that could be useful there. Other than that, I don't know. I don't know. Okay. Yeah, that's one. Where can you buy the dev boards? Where can you buy the dev boards? Okay, the dev board is a up squared and we on the slide you will find a link to our documentation. And on there it will have the exact dev board that we use. You can order it online and we have a flash image that you can just flash on it and then you're ready to go to play with this. Okay, I think there's no more. Thank you for listening.