Hi, folks. I think I'm going to start just a couple of minutes early because I got a lot of slides for you today. And, yeah, I'm going to try and keep up one slide per minute. But if it's unclear, of course, you know, shout out, just tell me to clear something up for you. Okay, so my talk from No Way to Oday, Weaponizing the Unweaponizable, I had to come up with a name that was going to be kind of catchy, and I think it actually turned out a little bit lame, but that's all right. I'm Joshua Wise. Joshua Wise, so, right, so here's, so kind of here's what I plan to talk about today quickly. I'm going to give you a quick little intro, figure out who I am, who you all are. We're going to talk about, briefly, we're going to talk about vulnerabilities kind of in the general case. I'm going to go across an old-style vulnerability that everybody here knows about, everybody learned about in CS 101. And we're just going to dissect it using these three points that I'm going to use kind of for the big one. Okay. Okay. And then I'm going to give, and then I'm going to give a case study of taking a very difficult vulnerability, or at least something that I think is a very difficult vulnerability, and I'm going to use this, I'm going to use these three points that I've developed to make something very difficult into something very doable to exploit. And briefly, I'm going to talk about kind of what went wrong, you know, things that could have been done differently that would have made this not, you know, that could have mitigated this. How did this happen? And I'm going to have a demo for you. And with any luck, I'm going to have time for a Q&A session. Otherwise, we're going to go to the Q&A room down the hall. So without further ado, let's begin. Okay. Who am I? I'm just some guy, you know. I kind of call myself an all-purpose embedded hacker. You know, I do, I've done HP IPAC stuff back in the day. At some point, I got roped into doing Android stuff. I'm one of the unrevoked guys. If you have a rooted Evo or rooted Incredible, you're probably running my code. And, right, so yeah, there you go, unrevoked. At some point, I kind of got roped into it. And that's, in a sense, that's where this presentation started off. I like to classify myself nowadays as a recovering software guy. I used to do software guy. Now I'm kind of trending towards ASIC design. And, of course, I'm buzzword compliant. I am working on my integrated master's and bachelor's in electrical and computer engineering at Carnegie Mellon University. Although, that doesn't really matter. Okay, so now that you know a little bit about who I am. Oh, by the way, I have a website, too. It's my first name, last name, concatenated.com. I think it's probably linked somewhere, too, although I didn't link in this presentation. A little bit about you now. So, that's a little bit about me. Who are you? So, hopefully, people here have a little bit of experience writing or running code in kernel space. Maybe. Maybe some of you guys have experience, in particular, with a Linux kernel and working kind of inside that infrastructure. All of you guys here are interested in security. And, in particular, you know, exploiting Linux kernel is what we're going to be doing today. Nobody here, I hope, is a script kitty. No code for you to compile here. I'm not going to be, I guess, I could potentially be convinced to release a binary that doesn't actually give you root. But I'm not releasing code today. But, however, in this presentation, there really should be enough, if you really wanted to reproduce this on your own, to be able to do that. So, that's kind of, hopefully, who you are. And, of course, you know, all of this is going to be all C-based. So, hopefully, you have a good understanding of what's going on inside the machine, even if you haven't worked inside of kernel space before. So, here's the vulnerability I'm going to be talking about today. So, we were looking through, we were looking through very early on. We were looking for ways to root the HTC Incredible. Because nobody had really come across that. Nobody had really solved that problem yet. And, we came across this exploit. CVE 2010-1084. So, this is what they called a kernel denial of service. You know, you could crash the kernel, and they wrote it as bad memory access with sysfs files allows attackers to cause a denial of service memory corruption. Okay, so it's a DOS attack, right? We're going to bring down the kernel, and so what, right? All of my stuff is transactional. All of my stuff is going to be saved. We're running journaling file systems. The machine crashes, so what? And, so this first showed up in 2.6.18. And, it got fucked. It got fixed in 2.6.23. That's a lot of kernels, you know? That's a lot of vulnerable kernels. To give you an idea of who's running things that aren't 2.6.23, if you're running an Android phone that hasn't been upgraded to Froyo yet, then you're running 2.6.29, and it hasn't been patched yet. This, every single Android phone that I know of that's not running Froyo is vulnerable to this. So, who here hasn't patched up to 2.6.23? Or, 2.6.33? You're brave. You're brave for admitting it. So, let's talk quickly about what's going on inside this vulnerability. How this vulnerability works. And, we're going to go into a whole lot more detail about it later. But, I'm going to kind of go for the brief overview from 10,000 feet of what's going on inside this that causes the machine to roll over and die. And, you know, how we can cause a crash even if we don't know how to exploit it yet. So, this one's really a standard one, okay? This lives inside the Bluetooth layer, inside this socket control layer called L2CAP. L2CAP is a subsystem of Bluetooth. And, the user can open sockets, can open L2CAP sockets just using the socket syscall. The same way you'd open any other socket, the same way you'd open a TCP socket. But, what makes this interesting is they decided they were going to export debug data about all active L2CAP sockets. Not through the interfaces that you'd get, you know, with netstat by looking through FDs, things like that, right? But, they decided, hey, this is the new Linux kernel. This is Linux 2.6 and we can provide debug info through sysfs. This is the new Linux kernel. And, it's not really sensitive information, really. So, everybody gets to read this debug info out of sysfs. You know, if you're running a machine with Bluetooth, then what do you really care about who's connected, about which connections are active, right? You're not really leaking sensitive data. It's a machine with Bluetooth, you know. You can walk around and see who else is in the room. So, what do they do? They take this buffer, okay? They take this buffer. And, for each Bluetooth connection. For each Bluetooth socket that the user has created. For each L2CAP socket. Actually, this exists pervasively throughout the Bluetooth stack. And, it was correct. And, the one that we're going to be looking at today is the L2CAP one. And, they just say, okay, I'm just going to keep appending onto this buffer that sysfs has allocated for us. For each socket, bam, we're just going to sprint F right onto the end of it. And, well, they didn't really bother to check for the end of it. So, how does this buffer kind of show up? How does this buffer live? Well, it gets. So, this isn't really a simple stack buffer overflow. Because, it gets the buffer from the frame allocator. Okay? And, we'll talk a little bit more about what that means in a moment. It's kind of a little bit of a twist. Because, we don't really control the contents of what's going through. Right? We only, we kind of get a fixed ASCII string that's sprint F'd on over there. Right? We really don't get a chance to control that. And, we don't really control how much we print either. Because, we don't really know how far into the string our target is going to land. So, let's take a step back here. And, I'm going to, we're going to analyze an old vulnerability. Everybody knows about this one. Everybody's favorite gets. And, we're going to think about it in terms of these three things that I mentioned. Let me go back here real quick. Target. Contents. Length. Remember that. We're going to be focusing a lot on that today. Target. Contents. Length. So, here's the old vulnerability. Okay? We have this program that was written for CS101. And, there's a, and it's set you at root because the professor is an idiot. And, told the student that in order to access the terminal, you have to be set you at root. I don't know. But, it happens. You know? So, we have our fixed length buffer. And, we're going to ask what the user's name is. And, we're going to gets on in there. And, we're going to print their name back. And, we're just going to say hello. This is, this is really easy to exploit vulnerability. Why is this so easy to export or exploit? Because it's got a controlled target. We know exactly where it's going to go. It's going to go onto the stack every single time relative to the same things every single time. Controlled target. It's very easy because it's a controlled length. We only have as many characters copied as I bang in on the keyboard. Or, into standard in or whatever it wants to be. Right? The length is exactly what I want it to be every single time. And, not only that, but the contents are controlled. I get to decide exactly what it's going to be every single time. So, let's go take a little look here. And, we're going to, I'm going to give you a little memory diagram of what's going on. And, I think this is kind of a useful visualization just as a refresher. I'm sure everybody here knows about how a stack smash works. But, I think it's going to be a very useful visualization. So, let's talk a little bit through this here. We have the user input something bad through gets. And, where does it go? Where in memory is this going to go? Let's take a look at this. I've drawn a nice little picture for you. By the way, by the way, these addresses, they're not really for any machine in particular. I just kind of made these up. Alright? Don't base any kind of exploit on this. Or do. And, it won't work. So, where's it going to go? We have the buffer here. And, by the way, lower addresses are towards the bottom of the screen. And, we have, so we have gets' stack frame. And, gets' is going to go stick things into buff. And, things go up. Alright? And, so here we go. So, we have the user that inputs something bad in here. Very, very long string full of the letter A. And, it gets' written. Normally, it would get' written into buff. And, this time it kind of gets' written through greeter's stack frame. And, then gets' written through greeter's return address. Greeter's return address gets' set, gets' reset to inside buff into that code that we've injected. And, owned. Right. Return address now contains the address of code in buff. Buff now contains code. Why did this work so well? Alright. Why did this work so well? Three controls. Attacker controlled target. Every single time return address gets' blasted. Right. Return address. Every single time return address is always on top of buff. Attacker controlled length. We always type in as many characters as we want get' blasted right up through there. We don't ever have to worry about blasting off the buffer. At the top of the stack. If we blast over C000, right, then we're done. Then we segfault. We're out of mapped frames. We're out of mapped pages. We're done. So, we don't ever have to worry about that. And, we have attacker controlled contents. We can write anything we want using gets' except for the null character and except for a new line. Okay. We want to overflow that buffer as long as our exploit code doesn't contain zero. And, as long as our exploit code doesn't contain zero. If our exploit code doesn't contain a new line, we're good. All right. Everybody ready? That makes sense to everyone? Okay. Let's talk about the meat of this presentation. So, here's what's going to go on in today's exploit. All right. In the exploit that I'm going to be talking about today. If you're following along at home, this is going to be in the L2CAP module. The function name is L2CAP sysfs show. And, you can see that. And, I think this bears a little bit of explaining. The sysfs stuff, whenever you open a sysfs file, it goes and allocates this buffer, right? Stir equals get zeroed page GFP kernel. Every time you open a sysfs file, it gets this buffer. All right. It doesn't necessarily run the print immediately. Okay. It doesn't call this show function immediately. But, as soon as you do the read, when you do the read for the first time, it calls this show function. L2CAP sysfs show. And, let me show you a little bit of what's inside here. For each L2CAP list, right? For each socket, and we're going to call it sock. String plus equals printf string. And, then a bunch of crap. What year is it? Guys, who does that still? Okay. This is a problem. This is a bit of a problem for us. We can see how this is going to crash the machine, right? This is always going to run off the end of that string if we have too much stuff in there. We said get zeroed page. That's going to be one page long. There's the overflow right there. Okay. But, you know, how do we control the target? How do we control? Well, we don't know what's going to be in the page after that. And, that says get zeroed page. I'm going to be talking a lot about pages and frames. Okay. I'll talk about that in a moment. But, how is the buffer allocated? How is that string allocated? The Sisyphus string comes from the frame allocator. That's that get zeroed page function. And, what comes after that? Well, some other poor bastard's frame that, you know, just kind of got unlucky and happens to be hanging out after the frame that we're going to smash. So, briefly, a little refresher on virtual memory for you guys. So, what's a frame? Don't confuse it with a stacked frame. I think we're pretty much done talking about stacked frames for today. Okay. A frame is a physical memory backing of a page. Okay. A page is a chunk of virtual memory. Pages do not necessarily have to have frames associated with them at all times. Okay. And, so, I've given you a little drawing here. So, we have two processes running on our little virtual system. One of them, and both of them, by the way, have stacked data and text. Okay. These are, and they each have one page for each. Each virtual memory address space, you know, virtual memory address space is composed of pages, physical memory composed of frames. So, here's what memory looks like to process A. Text is there, and it's backed by a physical memory frame. All right. Data is backed by a physical memory frame. Stack is backed by a physical memory frame. Process B is the same process. Okay. It's just the same program, which means that it can share the same text frame. Okay. So, the page, both of those pages, map to the same frame. And it's read only, so it's okay. Process B hasn't been run in a while, and data has gotten swapped out to disk. Sorry, Process B. Its stack is still in memory. All right. So, this is the process B. And so, what's important about this? What's important about this is that the Linux kernel has both physical frames, it has a whole bunch of physical memory mapped into the address space. And it has whoever used to be running, whatever user land process used to be running, it has their user address space also mapped in. And this is to make context switches faster, blah, blah, blah, take an OS course, right? All right. But the upshot of this, anyway, is that you need user memory to be accessible if you want to do things like read, write, iOctal. And you need kernel memory accessible if you want to do frame allocations, you know, to get a DMA buffer for the hard drive, for instance. Okay. So, that's right. So, that was controlled target. Now we know how the buffer allocation works a little bit. Let's talk about the next issue of our three controls. The issue is obvious. Crash is inevitable. Control number two is the length. Okay? So, we know that writes take place through that Sprintf to a strange place. And we can't stop it. We don't determine how long it goes because that Sprintf is just a fixed length format string. And even if the thing that we want to smash is in the middle, it will just kind of keep going off the end. So, we hope, we can't stop it before it overwrites something else important later on. Okay? And here's our third control. Controlled contents. Okay? What are the contents of the buffer? So, remember in our elementary exploit, the contents were the letter A a whole bunch of times, some shellcode, and then a return address. But we don't control that. Okay? No data comes directly from me in this exploit. No data. All of this data is formatted through Sprintf. So, what's the... What's going on here? We're zero for three. How are we going to exploit this? Well, okay. It's time to start controlling our environment. The environment is not... You know, the environment isn't set up for us to control. It's time for us to start taking control. And... So, let's go for it. How are we going to control the target? Well, let's go for an old-fashioned heap spray. The... What's the idea behind heap spray? The idea behind heap spray is that we're going to... We're going to scatter a whole bunch of things around memory. And we're going to just hope and pray that the things that we want to... The thing that we want to smash shows up after the buffer. And the way that's going to work is we're just going to litter memory with things that we could potentially smash. Um... So, uh... What are we going to spray across the heap? By the way, target practice. EMR Havel Heavy Industries. With EMR Havel, your target is our target. Um... So, the first idea for things that we could smash. A kernel stack. We know how to smash stacks. We've done that before. It worked, you know, it worked really well in that little easy gets exploit. Um... We know how to smash kernel stacks. We just overwrite a return pointer and let's go. So, uh... So... Let's take... Let's assume for a little while the best case scenario. Okay? Uh... The kernel stack, alright? Um... The kernel stack is going to be in the frame after the sysfs page. The heap spray worked. So it goes there. Alright? And the other best case scenario is that we know what process ID that kernel stack belongs to. Okay? So we know who to yield to to make this execute. Given that... Given those... Given those two kind of best case, what happens, right? What does a kernel stack even look like? Um... I got a picture for you. Like... So like other stacks, like every other stack in the world, a kernel stack has stack frames. Don't confuse these with physical frames. Kernel stacks live in physical frames, but they have stack frames. Um... Like other stacks, a kernel stack has stack frames. Alright? So what are we looking at here? We have... We have some guy's stack there, right? This is... There's a thread control block. The stack grows down. So we go down from the top of the stack frame. Okay? There's more stack frames, register save space, and then a return address. Return address! I like return addresses! I can trample on return addresses. Okay. There's a bit of a problem here. There's a bit of a problem. Um... Kernel stacks have another thing in them called the thread control block. A thread control block kind of describes... A thread control block describes what state the thread is in and threads, I guess in Linux, we call them processes. But a thread control block describes what state the thread is in. Whether it's runnable right now, who its children are, who its parent is, what address space it lives in. A kernel stack has a thread control block attached to it. Every thread has one kernel stack and one TCB. By the way, these addresses are bogus. So let's take a look. Okay? Let's take a look at what happens when we write when we're going to smash this string buffer. Um... So we have our sprintf. This is kind of a simplified sprintf and it's got all of our text and it's going to go from the string up. Um... So in order to get to that return address, we've got to hit the thread control block. Okay? Whenever we write, the thread control block is clobbered. So, by the way, this isn't the end of the world. Okay? This isn't the end of the world if we clobber the thread control block. And here's why not. If we have control of the data, then we can write something that would have been a valid thread control block into that spot. And it might not have been valid enough to keep the system running But it would be valid for long enough, you know, for us to switch to that thread and hit his return address and take control of the system. And then drop a rootkit and reboot the machine or whatever. Right? Well, we don't have control of the data. This time we just get ASCII characters and not even ASCII characters that we control. And best I can tell, a thread control block, well, you just can't do one of those with those specific ASCII characters. So this is dead in the water. Alright? This KStack idea, not gonna happen. Well, what else are we gonna do? There are other things that go in physical frames. But the big important thing, I think, that goes in physical frames is controlled by the slab allocator. Alright? The slab allocator is a central memory allocation structure in the kernel. And they switched to that actually probably late 2.4. And the idea behind the slab allocator is in a kernel you create a lot of objects that are about the same. Okay? You create a lot of objects that are about the same sort of thing. And so what we're gonna do is to reduce fragmentation of having a whole bunch of differently sized objects, we're gonna localize every object that's similar in memory. Okay? Every object of the same type we're just gonna pack into a frame and now we have frames full of these objects. Right. So these caches are frame sized. Sometimes they can be two frames long, but there you go, right? And so not only is this better in terms of memory fragmentation, but it's also better in terms of performance because of the algorithm that they use. So this was originally designed by the Sun Microsystems guys. I think it was probably Jeff Bonwick. Jeff Bonwick does everything. He's the guy who described this in a paper and they use it in the Solaris kernel and they have some paper showing like a ridiculous speed up under load in the kernel. So let me show you a little bit about inside the slab allocator here. What's inside of a slab? Okay? So, by the way, each of these lines is a frame boundary. I don't know if you can read that text there. But each of those lines is the boundary of one physical memory frame. And the way these slabs work is you have a fixed number of slots in them. In this case, these are huge objects. One kilobyte large. But you fit n of them into a frame. Okay? And so from somewhere outside in the world there's a pointer to the first free slot in each slab that has free slots. So when you want to do an allocation you go, okay, I'm going to follow this first free slab pointer to find a slot that I can allocate. And so now, where do we, now sooner or later we're going to have to update that pointer so each free slot has a pointer to the next free slot available. And since they're all the same it doesn't, we don't have to do any kind of best fit allocation, first fit, no, they're all the same so we just take the absolute next slot available or if there's nothing left right, the pointer goes to null there's nothing left available in this slab then then we just take this slab out of the list of slabs that we can allocate from. And the idea is that we want to fill slabs and we want to keep, and we want to empty slabs also. We want either full slabs or empty slabs so that we can release a frame back to the system. So where's the list of slabs available? Slab metadata is stored in a slab. It's a whole bunch of stuff of the same type. Well, where's the metadata for that stored? It's stored elsewhere in memory, blah blah blah. But, okay, so what's important about this slab thing to us? Why am I spending so much time talking about it? What's important about it to us? Well, there's no header on the front of a slab. The first thing in the frame in a slab is the object, alright? Very first thing, we can see it right here and my drawing is perfectly accurate because drawings are always accurate, is an in-use object, okay? This is convenient for us. This is really convenient. We can just kind of drop that string there. That would be a good place to drop that string, I think. Alright, who uses slabs? Well, I'm not sure if you can read that text from all the way in the back, so I'll read it for you. But every kernel subsystem uses slabs, alright? Every kernel subsystem uses slabs. So I've done a little grep inside of the Linux kernel for every time we call this the main slab entry point. Kmem cache alloc. Kmem cache alloc, and they call it cache alloc because they're allocating caches. They're allocating objects from a cache. So how many times does that show up? Well, there are 305 times in the Linux kernel that we call Kmem cache alloc. You know, that's not that they're all unique, but, so how many different types of slabs are there available? Well, the developers very nicely gave us some debug info that we can use, okay? And you can try this at home if you want on your own Linux machine. You can cat slash proc slash slab info. Actually, there's some very interesting data in there. And there are, as it turns out, on my machine sitting back in my dorm room at school, there are 183 different types of slabs. Well, if any luck, one of those is going to be an easy target, right? I'm going to pick completely randomly something that just so happens to work. And let's hit file descriptors. It's very easy to create. Why did I choose this, by the way? It's very, very easy to create file descriptors. And each, you know, from a user space perspective, a file descriptor has a number, right? FD number. That's what you get back from the open sys call. But those numbers have to map to something in the kernel. And they get a whole structure associated with them. They're called struct file. And unsurprisingly, there are a lot of them in the kernel so it's really a good match for a slab allocator. So we store these struct files in slabs. What's a struct file look like? Well, I'm going to give you the first couple of fields there that are interesting to us. There's a union that's this linked list which the kernel guys decided to call fu. And really it's two pointers for all we care. There's this path structure that's saying this is the path on disk that we opened it. And morally, that's also kind of two pointers. There's this other pointer here called fop. Anybody know about this? This is the, so every time you open a file, files can have, files are many different things on a Unix machine. Such as pipes, such as special devices, and each special device has its own list of file operations that can be performed. Because not everything is the same. We're not running a plan 9 machine. So this is a pointer to the file operation structure for this specific file, for this specific FD that we have open. And what's, by the way, what's the file operation structure look like? Well, it's just an owner and a list of function pointers. So following fop, okay, is fcount, the number of times it's open, fflags, some generic BS about the file descriptor, and fmode, the mode that it was opened with, and then there's like a whole bunch more crap after that too. But, so let's take a best case scenario here. By the way, why is this the best case? This is the best case because it's at the beginning of a frame boundary. Okay? It's right there ready for us. Okay? This is why this is the best case for us. It's at the start of a slab. There is no slab metadata there. And, you know, just kind of for reference here, each block in here is one pointer size. Just so you know, right? To give you an idea of scale. By the way, nothing else in this presentation is to scale. So what's the really, really best case? Well, the really, really best case is that our string just so happened to line up before our struct file. Okay? It's at the start of a slab and it came from the same, and getZeroedPage comes from the same kernel pool that the slab allocator grabs from. The slab allocator uses getZeroedPage or something very much like it. And the sysfs buffer allocator uses getZeroedPage. By the way, I'm going to talk a little bit more about that frame allocation mechanism in a moment. Okay, so what's a struct file look like? This is what it is, right? But, and I've darkened this time the parts that the kernel can survive for a little while with being smashed. It's not, you know, the kernel isn't going to survive forever. If you do a lot of operations on this file, it's going to be bad. If you exit the program, it's going to be bad. If you don't clean it up later, it's going to be bad. The kernel's probably going to crash. But there's only one pointer in here. There's only one member of this struct that really matters, you know? And that's fop. Great news! That's really good news, okay? That means that we can pave over this struct file if we want. Okay? We can pave over the beginning of it, we can pave over the end of it, as long as we get some thing that we're okay with in that file operation structure. Okay? As long as we get something that we're okay with in that file operation structure, the rest of the struct file can be smashed. For a little while. Okay, we're one for three. Let's kind of get back to why we're doing this here. What are the three controls that we're going for? We're going for length, we're going for contents, attacker-controlled contents, we're going for an attacker-controlled target. The length is not an issue anymore. Alright? We still don't have control over the length, but we found some place that it doesn't matter. We can go over by a little bit. Not, you know, we can't go over into many, many struct files, but if we kind of open the number of sockets very, very carefully, we can go over by a little ways without causing the system to blow up and die immediately. So what's next? We don't have length. Let's try and control the content. Alright. So here's the code that we're going to try and exploit again for your reference here. String plus equals sprintf, str, and then a bunch of format string for those of you in the back. So we can't write arbitrary content with this, right? ba2str always produces a MAC address for us. %d always produces a decimal. %x always produces a hexadecimal. All of this is controlled by them, not controlled by us. The good news is that we can open sockets with the properties that we want, right? So it's very easy to predict the content. So if we open a bunch of sockets directed to nobody, bound to no socket, with no data pending, blah, blah, blah, blah, blah, then if we cat this file, we're going to get a lot of stuff that looks a lot like that. Okay? So we've got a bunch of sockets with no MAC address, null MAC address, two, zero, hex, blah, blah, blah, repeated a bunch. How many times is that repeated? That's repeated as many times as we want. Okay? What does this mean? What does this mean? Data, all right, so data, this means that data that looks like that is going to end up in the file structure. If we go down this path, data that looks like that is going to end up in the file structure. So we'd better make the most of it. And, what does that mean for fop? Well, it means that a substring of that, four characters of those, if you're on a 32-bit machine, four characters of that is going to end up in fop, no matter what happens. All right? Well, what can go in fop? Anyway, what does it make sense to put in fop? And can we have this go in fop? The stuff, one of those substrings, okay? One of those substrings. Can we put that in fop safely? The only thing that matters from right now is to make sure that something that makes sense shows up in fop. Something, it's the only thing we care about in struct file. Without something that makes sense showing up in fop, the gig is blown. All right? We're done without it. So we'd better make it good. How are we going to do this? Well, fop is just kind of a pointer into the kernel's address space, okay? fop is a pointer into the kernel's address space. It's dereferenced by the kernel, and since the kernel thinks that it controls it usually, it doesn't do any checking on it. That's okay. That's all right, because the kernel is supposed to be controlling this. What's interesting about that? The kernel's address space is a strict superset of the user's address space on x86. On x86, it is a strict subset of the user's address space. And so that means that it would be okay if fop was a pointer into user memory. It would be okay. It doesn't have to be a pointer into kernel memory. So let's come up with a little bit of a game plan here to get this pointing to something reasonable in user memory. We're going to take this string that we saw a moment ago, that string, and we're going to map all of the substrings of it to become something valid of an fop target. Okay? So for each of these, so the first substring is 00 colon 0. And in ASCII that's going to be hex blah blah blah we trim off the end and make sure that that address points to a valid fop target. Okay? 0 colon 00, right? And we're just going to keep walking through the string for every member there, bam bam bam bam bam. We're just going to keep throwing it along there. And we're done, right? We have this string sitting here. We now have data written there. And we've mapped that there. This is mapped in user space. We're using mmap. We control that. We have, I don't know if you can read that from way out back there, but the pointer's inside there. So owner is null. That's okay for an fop structure. And let's think about some of those other pointers. Some of those function pointers maybe llseq should go to attacker ring 0. Maybe read should go to attacker ring 0. We're done, right? Well, not so fast. In reality, it probably looks a little more like that. Maybe there's going to be another frame in the way there. And now instead of having fop point to this structure, we have just some random bogus crap pointing to this structure. fop has not been touched. We haven't done yet. Okay. Three controls. Length, content, target. Contents aren't controlled, but we've predicted them. We've got length and we've got contents handled. Let's hit up the target. Let's be buddies. So how are we going to control the relative placement of these frames? We know that we're going to The whole idea behind this is the heap spray, remember? We talked about that before. But we can't allocate that many FDs. We can allocate 16,000 FDs, but that isn't going to get close to the 128 meg of RAM in the VM that I'm going to be using today to demo this for you. And it's not even getting anywhere near the 4 gig of RAM that a small server nowadays has. So heap sprays are hard now. So let's talk a little bit about how memory gets allocated. I promised you earlier that I was going to talk about this. Physical frames are allocated using this concept called the buddy allocator. The buddy allocator is called that because it pairs up frames and it pairs up groups of frames and it calls them buddies and merges them. And this is a really, really old algorithm. This is from 1963. According to Neuth, who was the one who wrote about it, Markowitz was the guy who wrote it down and I think it was actually invented in the late 50s. So this is a really old algorithm, but it turns out that if we use things like slab, this goes really well with slab. Because we're not having all of that fragmentation and memory of different sizes. So the implementation of this lives in Linux, MM, PageAlloc.c. This is 4,000 lines of really, really, really well-tuned code tuned to the weird cache infrastructures of Alpha, x86, Arm, Itanic, run in goddamn fear. You don't want to read it. Get a copy of Understanding the Linux Kernel. It's got a good description of it. That book is my god now. But what's important about the buddy allocator anyway? So with a run-of-the-mill frame allocator that's just grabbing frames, there's no real ordering to those frames. If it's just grabbing a whole bunch of frames, there's no real ordering to it. So this is going to inject determinism predictability into frame allocation that otherwise we'd just be picking frames at random. We'd really have no way to determine what frames are going to be next. And the reason why it does this is it can allocate groups of frames of different sizes and when it's able, it'll try and put a bunch of frames one-off frames together so that it leaves as much memory wide open for big groups as possible. The implementation details totally beyond the scope of this talk. I guess if I hadn't talked about slab earlier, I could talk about it. But I really think slab is cooler. So you guys can go look up the body allocator on your own. Alright, what are we going to do? How are we going to make this work? We're going to try and use this localizing technique here. And in brief, we're going to go fill up memory and that means that everything that would have caused a discontinuity, it's going to get paged out. It's going to get paged to disk. Every other program on the system, paged to disk. What are we going to do to generate contiguous chunks? Well, we're going to free stuff. We're going to do this little alloc-free-alloc dance to get contiguous chunks of memory. And what are we going to do to allocate chunks of memory for our struct files? Well, we're going to free a little bit in there. And that's going to happen. We're going to allocate our buffer page. Allocate our buffer frame, I guess I should say. And how are we going to do this? Remember, this happens when sysfs opens the file. And the write hasn't happened yet. You can do the allocation without causing the smash to happen. This is critical. Alright, we're going to pick up more memory for struct files here. Ready, aim, fire. What do we do? Memory starts off kind of looking like this. It's fragmented. There's other people's memory all around. Luckily, we can allocate about as much memory as we want. And that's exactly what we're going to do. All of the system's memory minus the stuff that the kernel needs goes to us now. You know, there's a lightly used machine that nobody's really paying attention to if you rootkit it. So it's not really using there's not a whole lot of other stuff using memory. So everything gets paged out and we're going to do our little free and allocate dance to make that all contiguous. You couldn't really see a change there. But now the frame allocator has those all kind of lined up such that if we free four in a row, we get four physical free frames in a row. So what are we going to do next? We're going to set up our files, our buffer in our files, and... own. We're three for three. Remember, three controls. Attacker-controlled length, attacker-controlled contents, attacker-controlled target. We made these controlled by us by deterministically permuting memory. And what happened? We owned the machine. What went wrong? What went wrong? I'm going to leave this up here for you. I'll let you read it. There's a comment here right after we do the read dance. Right after the read happens. This code works fine with page size return, but it's likely to indicate truncated result or overflow. So what do we do? What does the kernel do when there's an overflow? Let's struggle along. Guys? Guys? Demo time? I have a VM here. I can't actually see it. So we're going to have to hope. How do I get out of keynote here? There we go. So I've got GDB attached to my VM here. I'm kind of doing this blank. Because I can't see the screen very well. And here's what I'm going to do. I'm going to... Where's my mouse? So here's my VM. It's alive here. And I'm going to stop the VM and I'm going to break target ring zero. Can everybody see that? Okay. Well, the people in front will be able to see it and I'll narrate otherwise. Sorry. Oh. I can do a little zoom thing. Does that work? Oh, look at that. Damn it. All right. Sorry. I'm a little pressed for time. So I'm not going to spend much time playing with that there. All right. So here I am. And now the VM is running again. It says continuing there. So now I'm going to run my exploit. Uh-oh. Can I type? Yes. And let's go watch that... Can I get these both at the same time? All right. Now we've got my screen here and we're stopped in the middle of the screen. We're in the debugger. We're stopped in GDB here. And I'm going to do a little backtrace for you. What are we looking at in this backtrace? If I had any clue where my pointer was, I would... Yeah? No. I would be showing you. I would be zooming in for you. Oh, yeah. There we go. Look at that. That looks like a kernel stack, doesn't it? How do we know that's a kernel stack? That address there is... I can't really read it. And that's... That's kernel memory, okay? Let's print something else. File arrow fop. Did that work? What's that file operations? That's in user memory, okay? That address. That address is an ASCII string. We have now injected stuff into the kernel. Okay? And, uh... Let's finish it off here. Continue. Yeah, well... That's what we get, huh? I have no idea... Oh. That's it. Um... What's the conclusion? The conclusion is things that are hard to exploit can be made easier by thinking about controlling your environment. If we think carefully about control, that's what all of this is about, controlling execution, if we think about it in terms of attacker-controlled length, attacker-controlled contents, attacker-controlled target, these three things. Just because it's not easy, that doesn't mean that it's impossible. Okay? Kind of in this whole thing here, we've conjured the world to be the way we want it to be. This isn't trivial to do. Okay? It requires kind of an intimate understanding of the kernel, but this isn't something that's impossible to do. If we kind of sit down with a book understanding the Linux kernel, if we take a problem-focused approach and solve, then we can do it. There's another little conclusion here, and I guess this kind of brings us back full circle here. Dear phone vendors, we will win. We have physical access. Root on these phones will be ours. Please, stop your crusade from keeping me that's keeping me from using my own phone. Thank you. Thank you. Thank you. Any questions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .