Hello, everyone. I'm Samia, Samilam, Jordan, whatever the fuck you want to call me. Today I'm going to talk to you about jailbreaking the Nintendo 3DS. And you might be wondering, okay, why does this matter? Well, truth be told, it really doesn't. It's just kind of a way to piss off Nintendo. And the reason Nintendo doesn't want us to hack their consoles is because, you know, they want to sell games, they want to make money off their games. And unfortunately, once you hack these consoles, it makes it possible for people to play games for free. Not really happy about that. The thing is, it's actually also, like, a really interesting target in terms of security properties and in terms of hacking stuff. So we're kind of in the middle here of, like, trying to do interesting things, but also, you know, bad results happen. So I'm not trying to make people have the ability to steal games, but it kind of happens. Anyway, first thing about talking about the hacking the 3DS is kind of introducing the 3DS. We're going to talk about the 3DS. What is this? 3DS is a game console. It was originally released in 2011. There is a new one that was released in 2014. They're essentially the same thing except the new 3DS, which, you know, is a great name, has twice the CPU cores. It has higher frequency. It has more memory, basically twice the amount of main memory. But beyond that, they are basically the exact same thing. They are running the same operating system, which is something that I'm just going to get into. It's a really cool microkernel architecture. And they both have, in addition to the main CPU, which is what runs your games and stuff, they have a secondary CPU, which is the ARM 9 CPU. So ARM 11 here is what you can see in the CPU box here is basically what is going to be running all your games, all your apps. Basically anything that hits the screen, anything that you can interact with is going to be running on that CPU. On the other hand, you're going to have the ARM 9, which is the console's security slash IO CPU. And so the ARM 9 is responsible for doing a bunch of security tasks and kind of brokering access to a bunch of hardware. So in this case, I kind of, like, showed some hardware devices here. This is not an exhaustive list. It's just a few examples that will come in handy later. But so the idea is that the ARM 9 basically has access to, you know, everything. It has the keys to the kingdom. I mean, it doesn't literally have the keys, actually, because the keys are on, like, this crypto hardware blob over there. But it has the ability to talk to the crypto hardware blob. It has the ability to encrypt and decrypt content, which is really all we care about. And then it also has the ability to access the, you know, the, you know, the, you know, the, you know, the, you know, the this NAND chip, which is all the, you know, permanent storage as well as the SD card. While on the other hand, if you take a look at the ARM 11, the ARM 11, first off, does not have access to ARM 9 internal memory, which kind of makes sense. But it also does not have access to the crypto hardware. It does not have access to the NAND chip. And so basically any time that the ARM 11 wants to access a file on disk anywhere, it has to ask the ARM 9 very nicely to give it access. And so that gives the ARM 9 the ability to, you know, broker access to resources kind of like an ARM 9. Now, taking a look at what actually runs on the ARM 11 is, as mentioned, a very cool, I think very cool microkernel-based architecture. And so the idea is that you will have as little code as possible inside of the kernel, right, because that is going to be your highest level privilege on that CPU. You want to have as little code in there and ideally have the most, like, all your drivers and stuff in user mode. And so that's what you're going to see to the right here in the base memory region is you're going to be having a bunch of processes which are going to be called system modules and are essentially just user mode drivers. If you think of a monolithic kernel model like, say, Windows, you would actually have all these drivers live inside the kernel. And what that means is that whenever you compromise one of those drivers, you gain access to the entire system. Whereas here, if you compromise a driver, you just gain access to whatever that driver had access to. Because the way that this operating system works, it actually gives as little privilege as possible, you know, principles and stuff like that. And so that's what we're going to see. And so what that means is that, for example, a game is only going to have access to a small portion of the system call table. Same thing, you're going to have, in addition to games that are running in the application memory region, you actually have applets which run in the system memory region. And applets are going to include anything that can run at the same time as your game. So stuff like home menu, the web browser, the notes taking app, whatever, any of that crap can run at the same time. And so it's in a separate memory region. The whole point here is that you're going to have a system module that between the game and the home menu, you actually have access to the same set of system calls, which is fairly limited. As you can see, it's basically like half of all the system call table. But then if you take a look at one of the system modules, you're going to have access to the same set of system calls. In addition to that, you will have access to new system calls, which are going to be privileged system calls. Things that are going to, for example, allow you to create a service, advertise that service, and then in addition, you know, if you have an especially special system module, it might have access to a system call that is a little bit different than what you're used to. It's only accessible from that particular process and nowhere else. In addition to that, you actually need to have the ability to talk to these drivers, right, because they're not just in the kernel. They're in these little pockets of, you know, processes here. And the way that this works is basically any given driver, any given system module can advertise a service, and then through the kernel, a game can connect to that service and kind of talk to it directly. And the cool thing about that is that much like the system call filtering, you actually have a service that can service access list. So, for example, a game might not be able to access this AM SIS service, AM standing for application management, so it's a service that lets you, you know, install and uninstall games or applications or whatever. And so it makes no sense for a game like, you know, Zelda to try and install and uninstall new processes. However, it makes sense for a home menu to have access to that, and so you have this very granular level of privilege control on this system. And what that means is that even if you compromise a game, you might not be able to access that service. And so, like I said earlier, the ARM 9 handles a bunch of the, well, the ARM 9 does handle like a bunch of tasks, such as crypto tasks, as well as brokering access to physical storage. And so you actually have to go from one process to another. And so, like I said earlier, the ARM 9 handles a bunch of have to go from one process to another, and then to the ARM 9 to complete certain tasks. And so you have like this very deep level of like, you know, different levels of privilege that kind of like live one on top of the other, and it's not as simple as just, you know, user mode, kernel mode, and then the security processor. There's actually different layers and different levels of privileges between those. So then if we take a look just at this physical memory separation, because as I mentioned, you know, you have this application memory region, you have the system memory region, you have the system memory region, and then you have the system memory region. So you have the base memory region, and so these are actually physically separated memory. And so you have the FC RAM, which is the main bank of RAM, so that's going to be 128 megabytes, and it's actually separated into these three regions, such that whenever you allocate memory, virtual memory, like the actual physical backing memory will never, you know, go from one region to another. If you allocate memory from a game, it will be in the application memory region, it will never end up in the base memory region, and that might seem kind of trivial, but it will come up later. And then the thing is, you know, kernel for the ARM 9 is also, it's going to live in the ARM 9 internal memory, so you can't actually mess with it from the ARM 11, and then WRAM is going to be what contains all of the memory that pertains to the ARM 11 kernel, and yeah. And so the cool thing is with this kind of, like, really deep security model is, in theory at least, compromising the whole system should take a number of exploits, right? First off, you need to actually even get code execution on the machine, which is non-trivial because, you know, Nintendo is going to be like, it's not, it's not Apple or Android, like, it doesn't just give you the ability to create your own apps and, like, run into your console, so you need to actually first, you know, compromise an application. And from there, you'll be, you'll have, you might have code execution, but it'll be unprivileged, and so you kind of want to escalate your privilege to get more attack surface, and so one way of doing that is to compromise a system module. And from there, you might have access to, say, more system calls, and maybe those system calls are going to be more vulnerable than other system calls, and then maybe you can use those to compromise the kernel, and then from there, you have the complete attack surface into the ARM9, which is a security processor, and assuming you compromise that, you'll get the total control. Of course, you know, you have these kind of arrows to the left to kind of signal that, yeah, this is in theory, in practice, you can kind of go from, like, number one to number four. Sometimes I know someone has a bug that just goes straight to number four, but in this case, we are actually going to explore a bug chain that does every single one of these steps, and so it's kind of unnecessarily complicated, but I do want to show, you know, that in theory, it's security, model can be really cool and actually be really effective, and yeah. So first thing is actually getting codex issues in the machine. Just for a little bit of history, okay, this is supposed to be, like, animated, but I guess not. Okay, yeah, so for a little bit of history, there have been kind of two classes of entry points on the Nintendo 3DS, and, you know, one of these is going to be things like, you know, the, you know, the, you know, the, you know, the, you know, the, you know, the cubic ninja exploit from a couple years ago, which is a kind of bug that is trivial and really should not be exploitable on any modern platform, but is because Nintendo's, Nintendo kind of lacks a number of remote codex issues of mitigations on the 3DS, and, you know, it kind of makes sense. Like, things like ASLR, actually, you should really have that on 3DS because it doesn't really cost much in terms of performance, but then you don't have stack cookies, and that kind of makes sense in the context of a game because you don't want to sacrifice performance just for security, and the thing is, you know, 3DS, you also have these web browsers, right? It has the actual web browser applet. It actually has the YouTube app, which is just like a web browser with, like, a fancy coat of paint, and, you know, the thing is, from those, you can really trivially bypass these mitigations anyways. Like, no one believes that a web browser exploit is ever going to be stopped by ASLR or stack cookies, and so kind of the conclusion for that is even though all these bugs that are trivial are still exploitable on 3DS and really shouldn't be, at the end of the day, the threat model that Nintendo needs to adopt is that, you know, it's going to be a really, really, really, really, really, really, really, you know, user mode will be compromised, right? So they need to base themselves on that, and that makes it such that you end up with, like, a lot of low-hanging fruit, and the exploit that I'm going to talk about today is in the mCopy app. It's called the, you know, microSD network system transfer thingy. Basically, it just allows you to access the files on a microSD over network, and the way that that's implemented is an SMB server, and because SMB is, like, a notoriously secure protocol, of course, you end up with the ability to find vulnerabilities really trivially, and so the way I did this took, you know, like, an hour or so. I just grabbed PySMB from GitHub, modified it a little bit to actually talk to Nintendo's SMB server implementation because for some reason it just didn't work out of the box, and after that, added these six lines of fuzzing code, which, you know, just flip bits randomly, and if you want to take a look at what that looks like in practice, you have, you know, you have a lot of things going on. You have, you know, the 3DS is running the SMB server here. The fuzzer is running in the background, and it should just take a couple seconds, and then, of course, 3DS is going to crash because this, you know, super shitty SMB server actually, SMB fuzzer, I'm sorry, actually, you know, works surprisingly well. So at this point, you know, you have the 3DS has crashed. You know, the 3DS does not normally give you, does not normally, like, give you a registered dump, like a crash dump like that, but this is running custom firmware because it's how we do development these days, and so I'm not going to go too deep into detail into how that bug works because, first off, I'm not an SMB expert. I really literally just went with that fuzzer, found that bug, and exploited it, but to give a basic idea, basically, yeah, you have this, like, this is the packet that actually crashes the console, but in its normal form, so session set up and X packet, whatever the fuck that means, and it has a couple of, like, you know, data blobs in there, the NTLM response data blob, and what I mean by data blob is something that is going to crash the console, and it's going to crash the console, and it's going to crash the console, and it's going to have variable length, so as an attacker, I control the length of these blobs, and that is the NTLM response data blob and the domain name data blob as well. The domain name is actually just, like, a string that says work group, and the vulnerability is really trivial. Basically, it just checks the length of the data blob for NTLM response, and if it's not a one specific value, it's going to take another code path, and that code path just copies memory onto the stack into a buffer that has a fixed length, but, you know, like, a length that is controlled by the console, and it's going to crash the console, and it's going to crash the attacker, and, you know, obviously, you can just, like, you know, override the entire stack with that from a packet that is crafted just like this. You just make it such that the NTLM response blob is not 0x18, it's going to be 0x10, and then, you know, you make the work group blob size be, like, zero, like, you know, hundreds of bytes instead of, like, 10 bytes or some shit, and you actually just override the entire stack. You're able to override a return address. You're able to redirect the CPU's execution flow and jump into the return address, and you're able to redirect the existing code and get remote, you know, remote code execution, essentially. In practice, you know, I say remote code execution, but in practice, that actually just means ROP, which stands for return-oriented programming. Just real quickly for people who are not really familiar with that, what that means is we are not necessarily able to inject new code because we have this mitigation called DEP, which is, like, data execution prevention, and what DEP does is basically you're not just able to inject code into the process and just jump to it because the thing is that any memory that is writable will not be executable, and so that's actually, like, a really good mitigation that is actually enforced really strictly by Nintendo. There is, under no normal circumstance, like, under normal circumstances, there will never be memory in user mode that is both writable and executable, or memory that was ever writable will never become executable, and so that's really well enforced, and what that means is that instead of actually just injecting shell code and just jumping to it, you have to reuse existing code, and so that's what ROP is, basically just override return address, you jump to a tiny piece of code, make sure that then it will jump to another tiny piece of code, another tiny piece of code, another tiny piece of code, which we call gadgets, and from there you are actually able to do arbitrary computations, call arbitrary system calls, and do kind of whatever you want. The thing is, you know, my personal aspirations for hacking the 3DS were to actually run homebrew on it, which is, you know, games made by amateurs, applications made by amateurs, that sort of shit. The thing about the 3DS is that it's not just a game made by amateurs, it's a game made by amateurs, and so the thing about that is writing homebrew in ROP is not ideal, you kind of want to do it in actual native code, and so, you know, we are not able to create new executable memory, we're not able to reprotect writable memory to executable if we don't need to. What if we can just actually overwrite memory that is read-only? And the way you do that is through DMA, right? You have a bunch of devices that have access directly to memory, and, you know, the GPU is one of them, because the GPU needs to be able to, for example, read a data, and it's not going to be able to read a data, it's going to be able to read a data, it's going to be able to read a texture from, you know, read a texture from memory in order to render something, and also needs to be able to write a frame buffer to memory, right? And the thing is, the GPU actually has access to all of FC RAM, all of WRAM, all of VRAM, and that means that you can actually just use the GPU to render over code pages, right? In practice, it's not that simple, because otherwise you would just be able to overwrite the kernel, because even though it technically has access to WRAM, which includes the ARM11 kernel, you know, there is, like, a register that allows Nintendo to limit the range the GPU has access to through DMA. And so we're not able to just overwrite the kernel, and in fact, we're not even able to overwrite system modules or home menu, because the system and base regions are not accessible to the GPU. But because the GPU does need to be able to access the textures and stuff from the current game, we do have access to the first half of FC RAM. And so what that means is that if you think of, you know, this is physical memory at the bottom, you have virtual memory at the top. You have, this is going to be your text section, so it's readable and executable, not writable. Basically, you're just going to use the GPU to overwrite physical memory at the bottom, and then it's just, you know, well, because that's how memory works, it's going to show up in virtual memory, and you can just jump to it. And so basically, we use the GPU to render code into, you know, these physical pages, and then overwrite existing code. Right, and so Nintendo did not really like that very much, and they tried to kind of put a wrench into our plans. And the way they did that was they realized, okay, whenever people, you know, use their ROP chain to use the GPU to overwrite code with, you know, other code, they kind of rely on this specific hard-coded address. The reason being that, you know, this code page is always going to be at the same location in physical memory, and so we don't really need to do anything fancy. So their idea is, you know, this is before their mitigation was put into place. You just have these four blobs of code in virtual address space, and then it's just going to correspond really trivially to these four blobs of code in virtual address space. So their idea is, well, let's just jumble it up. If you, and that way, you know, as an attacker, if I try to write to physical memory, you know, because the order is going to be kind of jumbled up, and you don't know the size of the blocks, you don't know where the blocks, which order the blocks are put into and stuff, well, if I try to write to physical memory to the same location as before, it might show up in the blob that I want it to, or it might show up in this other block. And so that means I won't know the location I just wrote to, and that's, and so we call this, this physical ASLR, which, you know, PSLR for short, because that's really what it is. And the thing is, it's actually kind of a, kind of a shitty mitigation, because, you know, a good mitigation, you want to have mitigation that actually creates extra work for the hacker every time they write an exploit. The thing with this one is, well, you just kind of have to bypass it once, because it turns out ROP, as has been known for about 10 years, is Turing complete, and basically you can do arbitrary computations. So you can actually just do a for loop and search for the physical piece of memory that you want to overwrite. And so you can do arbitrary computations and then overwrite it. So we basically had to write this ROP chain once, and then you kind of just reapply it to every exploit. So, you know, not a great mitigation. And so what that means in practice is, if, you know, this is like the actual, you could just kind of write it on the computer, connects to the console, packs the console, and then we have correct fusion. We have like the actual homebrew menu running on the console, and you can just do that over network, over any console that is running 11.7 or whatever. At this stage, we have compromised, you know, unprivileged user mode. And, well, yeah, so that was the first step in our for chain, for exploit chain. And at this point, we want to somehow escalate privilege. Because the thing is, okay, so we have code execution. This is great. But we only have access to the basic unprivileged system calls, right? So in terms of attacking the kernel, it's totally doable, has been done several times, but ideally we want to have more attack surface. And likewise, if we want to escalate to, you know, we want to escalate to, you know, another, to like an actual system module, which might have access to more system calls, well, you know, mcopy, like this application we just compromised, only has access to a few of these services. So ideally we want a way to kind of migrate to another process that might have access to better privileges and such. And, well, it turns out we can actually kind of do that. Because, you know, I was showing you this slide earlier in terms of like what the GPU has access to in terms of DMA. I was saying it only has access to the actual application. So, you know, we want to have access to the actual application region. I kind of lied about that. It actually has access to a little bit of the system region as well. It just does not have access to the home menu's code section. It does have access, though, to the home menu's heap section. And so what that means is like any memory that is dynamically allocated by the home menu, I will be able to read and write through the GPU. And so from there, it's actually kind of trivial to just find some C++ object on the heap, you know, modify its V table, for example, and just have it jump to other code. And then you get code execution. Well, you actually just get ROP in the home menu. So that's kind of the annoying thing. It's like you can't use the GPU to get native code execution inside of the home menu. And so in terms of how that works in practice, we had to write this whole, like, service that runs in the home menu. And that's all in ROP. And, you know, but once again, like, ROP is turned complete. So you can just do whatever the fuck you want. And, yeah, so at that point, we have compromised, like, these two processes. And the thing that is interesting about that is, as you can see, even though we don't have access to the home menu, we have to write this whole service that runs in the home menu. So we don't have any additional system calls. We do have access to the services the home menu had access to. And one of those services allows us to, for example, kill processes and create new processes. And so the idea, then, is that we can actually just kill the mCopy process, because you have code execution inside the home menu process, and replace it with another process, and then use the GPU to take over that process, and so on. And so the idea, then, is that we actually have, you know, in theory, we have access to any, the privileges of any process that can live inside the application region that we can start. Right? And so that means that any game, any app, any surface that these have access to, we, you know, by, we kind of have access to as well. So we have, like, the biggest attached surface that we could possibly get from an unprivileged user mode, and that means that we can start looking into some more esoteric services, such as LDR RO. So it lives in the RO process, and basically what it does is, if you think of Windows, right, Windows has these DLL files, dynamically linked libraries, and it has these DLL files, and it has these DLL files, and it has these libraries. Well, it turns out the 3DS does as well. And they don't call them DLLs, they call them CROs, which stands for CTR, relocatable object, probably. Kind of just guessing at this point. And it is, like, an interesting process for us to go after, because it actually has access to a very special, a very special system call, which actually allows us to create new executable memory if we want to, which will come in handy later. And so taking a look at how it works, basically, you first allocate a piece of memory inside a map, right? And then you load your CRO into it from, you know, the file system or whatever, and then you're going to ask LDR RO to load it for you. And what I mean by load it is that because it's a DLL, it's supposed to be executable code, it's going to need to be re-protected to be executable at some point. As a process, I didn't have the ability to do that, but, you know, this LDR RO does. So the first thing it does is it actually locks it away from the application. And then it's going to apply, you know, dynamic linking stuff to it, which just means, like, relocating the memory from the application. And then it's going to re-protect it as being executable for the pages that that's relevant for. So what I mean by locking is that my application will not be able to write to that memory, which makes sense because we don't want to, well, LDR RO does not want us to be able to, like, mess with it as it's happening. And the thing is, because, you know, it's the application itself that is loading this CRO blob, the linker does have to be built defensively against, like, malformed CROs. And, actually, they did, like, fix a bunch of bugs there and made it such that, as far as I can tell, there's not a lot of vulnerabilities that you can just exploit by just giving a malformed CRO. The thing to notice, though, is, as mentioned, the application is the one that allocates the memory that is going to contain the CRO. So what that means is that the physical memory for this is going to be in the application region, which means, once again, we can use the GPU to kind of, like, mess with that CRO blob as it's being relocated. And, you know, I'm not going to go into too much detail about this, but I'm going to go into a little bit more detail about this. And, you know, that sounds like it could be a problem, because it was built defensively against malformed CROs, but what about CROs that are, like, kind of being modified on the fly? Well, it turns out it's not secure against that at all. And so if you look at the code, this is code that lives in the RO process and just kind of is part of the relocation process of this. The first thing is, basically, it's going to go through all the offsets in the header of the CRO and kind of modify them to stop being offsets into the CRO. And then it's going to go through all of the CROs and become actual pointers to the CRO. So it basically just, like, it just adds the base address of the CRO to each offset in the header, after checking, of course, that the offset is within the bounds of the CRO. Right? And so that could be fine. The thing is, this pointer that is going to be used later on by the RO process lives in physical memory that . And so what that means is that whenever it ends up being , you know, this is, for example, the pointer to the segment table in the CRO. Well, what you can see is that it's loaded from the CRO here, and then it is going to be used directly to both read and write memory. So as an attacker, if I can modify that pointer, I can start getting RO to read at arbitrary value, which is not great for them. And in practice, we end up with, like, three kind of weird, kind of weird, like, corruption primitives. The first one allows us to write an arbitrary value at an arbitrary value. The second one allows us to write an arbitrary location as long as there's, like, a byte that has the value 2, 8 bytes after the location we're trying to overwrite, and also, like, the location, like, 4 bytes after what we're trying to overwrite can't be value 0 for some reason. And the same thing below for the second primitive, except it has to be byte 3. And then all the way below, it can be any value there as long as what we're overwriting is not value 0. And then we're not actually just overwriting it. We are actually incrementing it with some other value. And then we have, like, these arbitrary read and write primitives. Well, actually, really just arbitrary write, but they're not really arbitrary in the sense that we do have, like, these weird constraints here. But, of course, it's not that hard to exploit this. In practice, what I want to do is get ROP inside of this process. In order to do ROP, I just want to overwrite a return address on the stack. And this is just kind of showing what I can and cannot overwrite based on these primitives. What you see in orange here are actually return addresses on the stack. And this is what I would want. And so what you can see is there's actually overlap between return addresses that I want to overlap, overwrite, and the locations in memory that I can actually overwrite. The thing is, we do have, like, this corruption target here of this corruption primitive here, which does allow us to overwrite memory. Well, actually, we can increment memory at an arbitrary location with much fewer constraints. So I don't need to have byte three for that. And instead, what I'm going to do is I'm going to use that to, you know, at this location, we meet all these constraints for this primitive. And so I can use this to actually place byte three, which I can then, you know, use this with the second corruption primitive to just overwrite this return address with an arbitrary value. So at that point, you know, there's, like, a little bit more to the actual full-on exploit. But this is the basic idea, right? It is pretty simple. And at this point, I will have a ROP execution inside this process, and I do have more privilege than I had before. And so what that means in practice is I just have access to a few more system calls, and I get to, like, look at them and see if I can use them to actually take over the kernel. So taking over the kernel. Well, now we have taken over this process called RO. We have access to more system calls. And one of these system calls that is actually really interesting is, like, this is a system call that is called control process memory. What control process, well, yeah, first off, this is an interesting system call because RO is literally the only process that has access to it. So in a sense, you can kind of think of RO as, like, an extension of a kernel that just, like, has this one very specific process, like, goal, like, purpose, and it has to use, like, this very special system call that was built just for it in order to achieve it. The thing about control process memory is it's really just the same thing as control memory, except that it can work cross-process as long as it's in the kernel. So it's really just the same thing as control memory, as long as you have a handle to that other process, and it has fewer constraints. One of the fewer constraints that it has, as mentioned, is that it can actually create or reprotect existing memory as being executable, which is really useful for us if we just want to run homebrew, right? We can just, we don't need to mess with the GPU anymore. We can just, like, create new arbitrary executable memory, and we can just, like, do whatever we want. But the other interesting thing about it is it also bypasses some of the restrictions that control memory has in terms of where it is allowed to map. So, like, if you have a control memory, you can just, like, map memory to. And what that means is that we can map the null page, which is address zero, which is something that is notoriously, you know, not allowed because a lot of bugs rely on the ability to place memory at address zero because a lot of bugs are just going to be dereferencing a pointer that is null and should not have been and was not checked properly. And so that's kind of interesting for us because then if we can find a null dereference bug inside of the kernel, which would normally just be a denial of service bug, then all of a sudden we might be able to elevate it to become an actual remote code execution bug. And so what is a good target for null dereferences? Typically, it's going to be memory allocation because if you have memory allocation primitive and you run out of memory or you just, like, try allocating memory, it's going to return null. Sinbez currently, you know, malloc or whatever does not check that the pointer is null, then all of a sudden you have null dereference and things become interesting for us. So take a look. So how the allocator in the kernel works for kernel objects, this is basically what it is. It's a link list. Well, so it's a slab heap. What that means is that basically for each type of object, you're going to have one memory blob that's going to be subdivided into sub-objects. And so basically whenever these sub-objects are not used, they're part of a free list. What that means is, you know, you have that list head and then each free object is going to link from one to the other and then allocating the object just means popping and, you know, popping a free object from this free list and putting it into, like, whatever you want to use it for. And then freeing an object is just going to mean pushing an object back into that free list. So what happens if we run out? Well, if we run out, we end up having the free list head point to null. And so whenever you allocate an object next, it's just going to return null. And all of a sudden, you know, we might have our null dereference bug that you want. Now, what that means is that, of course, the code that uses this allocation, this allocation function has to check the resulting pointer is not null, right? If it's zero, it should just throw an error, and usually it does. But what you can see is, like, that last example there, you know, it's allocating a new linked list node and it's checking that this node is not null. And if it is not null, it's going to initialize it to zero. The thing is, we're going to have to use it without checking. It just, like, it does this check for it to zero, but then even if it was null, it just kind of uses it without caring. So it is, like, kind of an odd programming pattern, but somehow it ends up being in literally every location that the kernel uses these linked list objects. And so the idea then becomes, well, if we can make the kernel run out of these linked list objects, we can make one of these linked lists be in the null page, which is controllable by us, but from user mode, and then, well, once we have that, we might be able to actually take over the kernel. So the question is, how do we actually make the kernel run out of these linked lists? Well, a good way to do that is actually just to look at other system calls and how they work, and one of them is weight synchronization N. It is a system call that is basically the same thing as weight synchronization 1. The only difference is that weight synchronization 1 waits on one object, whereas weight synchronization N waits on N objects, which I know is pretty obvious. The thing is, what I mean by waiting on an object is going to be something like a kernel object, like a thread. When you're waiting on a thread, that means that you're going to have your current thread wait until that other thread is dead, and then your thread is going to be woken up, it's going to get an event. You can also be waiting on an event object, on a mutex. Waiting on a mutex just means, you know, waiting until that mutex is not locked anymore. Basically. And we'll take N objects as input. In practice, that's up to 200 objects, and then it's going to wait on them, and as soon as one of them is signaled, it is going to wake up your thread and do whatever. Now, the question is, okay, it has to wait on these 256 objects somehow, so it has to keep track of them somehow. And the way that it does that, of course, is a linked list, right? So the idea then becomes, well, if we can create, if we can create as many threads as we want, and have each one of them wait on as many objects as we want, then we're going to be creating a bunch of these linked lists that all have, you know, as many as 256 objects in them, and then, well, you know, there's only about 1,500 linked list nodes that are allocated in the kernel, so after a few attempts, we should be able to actually get it to run out. And, yeah, so that is essentially what we're doing. We can actually trigger, we can trigger an LDReference bug that way. The thing is, it's not trivial to exploit necessarily, because, well, it turns out linked lists are using the kernel a lot, and the problem is, well, what if another process is trying to use, you know, a system call, because, you know, that's what processes do, and it needs to use a linked list, because, you know, that's what the kernel does, and it has run out, well, it's basically going to crash, because that other process does not have the null page mapped into it. So it's not great. And then the other thing is, even if our own process might have another system call, or even the current system call, because if you look at this, you know, it's going to just continue allocating new linked list nodes over and over again, right? And so even after we've triggered the vulnerability, we're still going to be keeping, like, we're still going to keep, like, allocating new nodes after this. And so the problem with that is, well, next time a node is allocated, even if it's in our current process, null is going to be returned, and if it's from another list, you're going to end up with two lists that, you know, collide into the same node. And you end up with, like, all these linked lists from the kernel that, like, are kind of mangled into one another, and that gets really messy really quickly, and we want to avoid that if possible. So the way to do that is basically to just kind of do this thing, I just call it, like, just-in-time freeing, but it's really not as fancy as it sounds. The idea is that as soon as you're going to have a linked list node, like, allocated in the null page, it's going to write data to that null page. And so because the 3DS has multiple CPU cores, you can basically just have one thread do that, you know, that null dereference bug by keeping, by allocating more and more and more linked list nodes, and then you can have the other CPU just, like, reading from the null page at all times, and as soon as it sees that, you know, zero has changed it to, like, a pointer value, be it, like, next pointer, previous pointer, and the object pointer, it's going to be able to take action. So as soon as it sees that, the first CPU core, so core zero, is just going to signal an object that another thread was waiting on, and that's just going to free a bunch of these linked list nodes, and then the next time we have an allocation, it's just going to use one of these linked list nodes that was just freed. And basically that's how we get around the whole issue. Now, the question is, okay, we're able to trigger this bug, and we're able to do it without crashing anything, which is great, but how do we actually exploit this to get code execution inside the kernel? Well, typically, like, I'm sure, like, people who are familiar with linked list bugs will know that basically you want to do this through the unlinking phase, and so just, like, explain how that works. So basically, imagine you have this linked list, right? So each node is going to point to the other than the previous one. So when you free, let's say, node two, what's going to happen is just you have to update the next pointer from node one and the previous pointer from node three. It's pretty straightforward. Now, the thing is, in our case, we actually control node two, right, we have full control over the next pointer value and the previous pointer value. And so let's say in this case that, you know, we say that next points to 0xBABE and previous points to 0xDEAD. Well, if we try to unlink this bad node, what's going to happen is that it's going to write the, you know, the next, it's going to write the value of previous to next and the value of next to previous, essentially. So that means, you know, you're going to be writing 0xBABE to 0xDEAD, and 0xDEAD to 0xBABE. And so that means that we can actually use this to write an arbitrary address to an arbitrary location, as long as both addresses, you know, point to actual valid writable memory. And so we end up with this primitive, which is obviously super powerful because we can use this to, say, overwrite a function pointer, and that's essentially what we end up doing because if you look at the code that actually invokes the linked list on linking right before freeing the kernel object, well, right after freeing the kernel object, it's actually going to make an indirect call for a VTable. So if you can overwrite the pointer that is pointed at by that VTable, then you have the ability to just, you know, jump to any location in code that you want. The only kind of annoying thing is that it turns out the free K-obj function is going to panic if it tries to free a null object, which is kind of weird because the allocation function does not really seem to care about the null object. It's kind of weird about returning a null object, but, you know, whatever. You end up being able to actually just exploit this by being, like, you know, vaguely tricky, and by vaguely tricky, I really just mean that, you know, you overwrite this VTable pointer. The only thing is you have to overwrite the VTable pointer with the address of a node that's going to be overwritten, and because of that, you have to actually make the null page be read-writable and executable, but basically what's going to happen is you just kind of put a piece of code in there, in the second node there that is going to just jump to some other location in user mode, and basically you just get code execution that way. It's not that complicated. At this point, we have access to everything in the ARM11. We have compromised the ARM11 kernel, which means that by extension, we have actually also compromised literally every other process that is running on the ARM11. At this point, we can kind of just do whatever we want. We can run whatever games we want. We can, you know, access all the hardware. That is accessible by VRM11, and it's pretty great, but it's not enough for whatever reason. We do still want to actually take over VRM9 because, you know, I guess that's cooler. It actually doesn't really give you access to much more. It does, like, allow you to write directly to the NAND chip, which is nice and definitely useful for other exploits, but, yeah, so we want to be able to do that, and again, we don't actually have the ability to write directly to ARM9 memory, but we do have the ability to talk to VRM9 through all of our devices, and so VRM9 is responsible for certain services, such as accessing permanent storage, as mentioned, but it also does other things, and one of those things is actually backwards compatibility. So the 3DS is actually able to run old DS games, and the way that that's done is by basically turning the 3DS into a DS in terms of hardware. Like, it puts a bunch of weird hardware registers, and that actually brings up a third CPU that is, like, kind of hidden, and then it turns the ARM9 into, like, a DS-mode CPU, and it's kind of crazy, and so the thing is, it has to be able to do that. In order to do that, it actually has to kill the current operating system and start another operating system that is going to do a bring-up for all this crap, and so the operating system that we've been working on so far is this native firm thing, firm being for firmware, presumably, but you have other firmware that can run on 3DS. You have SafeFirm, which is what runs when you do an update for your console, and you have TWLFirm, which is the one we're going to be interested in, and TWL is just, like, the codename for the DSi, so that's where that name comes from. And so in terms of... in terms of actually launching another operating system, what the 3DS does is it basically just has the ARM9 do everything. First, the ARM9 is going to load the memory, like, load the firmware image from permanent storage. It's going to load it into memory that we cannot alter from the ARM11, which is ARM9 internal memory, and it's going to use, you know, its crypto hardware to actually decrypt it and authenticate it and make sure it's, like, actually the Nintendo special sauce and not something that we've altered somehow, and then it's going to copy each individual section into where they're supposed to be. Because, you know, you have code that needs to run in the ARM9. That's pretty easy. It's already in ARM9 memory, but you also have code that needs to run in the ARM11, and that has to be copied to FC RAM, WRAM, and whatever. Now, the thing is, once it's done that, the ARM9 is not just going to start executing its own code. It is first going to tell the ARM11, hey, please start running the code on your end, too, because, you know, I need you to, like, do that as well. Thing is, we've compromised the ARM11, so we can basically tell the ARM9 to fuck off, and we can just, like, keep running whatever code we're already running and do whatever we want. And so that becomes interesting, because if you take a look at how TLEO firm works, basically it first has to load a ROM, like a DS game image from somewhere, and then turn into a pseudo-DS mode sort of thing, and it can actually load its ROM from three locations. It can either load it from an actual physical game card or from the NAND permanent storage, or, for some reason, it can load it from FC RAM. Which, of course, we have complete control over from the ARM11. And that becomes interesting, because, well, you know, it's a ROM loader. It's a file format parser. Well, there might be bugs in there, and it turns out there are. And so from the ARM11, we can actually mess with that ROM and kind of inject something. The only thing is, of course, DS ROMs are signed. So Nintendo actually checks that the DS ROM is valid before using it. And so that should kind of kill the idea, except that, for some reason, it does not work. It does not check the signature if the ROM is coming from FC RAM, which is completely baffling, because that is, like, the one location that we have control over. Honestly, I don't know why. I did not really care to reverse engineer into, like, why that happens, but it does. And so we are actually able to move forward with this idea. And so the DS ROM is going to basically contain two memory images that have to be copied, because these are the code images that are going to be run by the ARM9 and the ARM7, which are two CPUs of a DS. And essentially, yeah, the TWL firm loader has to basically copy these two ARM9 and ARM7 sections into whatever memory is going to correspond to the DS mode RAM address. So you have, like, this kind of formula to convert a 3DS physical address, well, a Nintendo DS mode physical address into a 3DS physical address. So two things to notice is, first off, it's, like, a kind of weird hardware compatibility mode, and so you only have, like, two bytes that are used every eight bytes, which is why we have to multiply by four. And then the other thing is, you can see with this formula that you can actually create any 3DS physical address as long as you have the right Nintendo DS physical address. Like, we can cover the entire memory space, even though that's not meant to be the case. And so because we have control over the address in Nintendo DS mode that the ARM9 and ARM7 sections are going to be loaded at, we can actually turn those into any 3DS physical address that we want. For example, memory that is used by the ARM9, to execute code. So, for example, we could maybe overwrite the actual ARM9 3DS mode code and take it over. That is, as long as there are checks that are not good enough. Turns out, of course, their checks are not good enough because they did not do an integer overflow check, which, again, kind of sad. And there's also no balance checks on the section sizes, so that means that we can have these, like, kind of crazy values of an address. Like, let's say we want to overwrite this address over there because, like, that is the ARM9, like, thread stack address. Well, we can just, like, create this fake Nintendo DS mode address and then give it its crazy size. This is, like, about one gigabyte of memory, so this would never be valid for a Nintendo DS ROM. And then if you check on the math, it's actually going to give you the address that you want, and it's also going to go through all these checks that we just mentioned here. And so at this point, basically, we end up with the ability to write about a gigabyte of memory to an arbitrary location in 3DS physical space. And that should be a problem because, you know, we actually don't have a gigabyte of memory to overwrite. The thing is, you know, I'm going to kind of skip over because I'm running out of time, but the idea is that we can just overwrite this physical address here. Well, the return address for this function because the actual memory that is being copied is being copied by tiny blocks instead of, like, big blocks, so you actually end up being totally fine. Now, the only thing is, for some reason, it's copying 2 bytes at every 8-byte boundary, and so you can't actually just, like, overwrite code because you can only overwrite 2 bytes, you know, on every 8-byte slot, which is super annoying. What that means in practice is if I want to overwrite this call stack, right, I can actually only overwrite the bytes that are highlighted in orange. And so in terms of making a ROP chain, that's, you know, not ideal, but we can make it work because the ARM9 doesn't really have depth or anything, so we can actually just use this to place an actual address, the address of, like, actual code that we control because that code can be in writable memory. In this case, we place that code into the Nintendo DS mode ROM header, and then we just overwrite one return address at the top there and make it jump to this gadget that's just going to skip a bunch of the call stack and then is going to return into this address, and we will get code extension on the ARM9. And, yeah, at this point, we have full control of the entire machine. You know, we can do this. We started with nothing, went over network, network, sent, like, one magic packet, has it, and then it just kind of, you know, gives you full access to everything, and at that point, you can read and write NAND, you can mess with a crypto engine and kind of do whatever the fuck you want. So thank you for your time. All the code for the exploits for this is available on GitHub. I want to give a special thanks to a few people, Derek, NetWill, YellowZ, Pluto, NeroWarp, and if you want to follow me on Twitter, that's my handle, Smelam. I'm not tweeting very interesting things, so, you know, please don't. And, yeah, have a good DEF CON.