1 00:00:00,000 --> 00:00:06,780 This is going to cover a little bit about Linux kernel exploitation, specifically using 2 00:00:06,780 --> 00:00:12,400 an uninitialized stack pointer. I thought it was an interesting exploit, but I'll get 3 00:00:12,400 --> 00:00:20,060 to that. So this is a 20-minute talk, so I'm going to blast through as much as I can with 4 00:00:20,060 --> 00:00:26,600 a quick intro. I'm Case. It's pronounced Case. The spelling is Dutch, and it's my grandfather's 5 00:00:26,600 --> 00:00:33,660 fault, but you can see me on Twitter there. I've been coming to DEF CON for quite some 6 00:00:33,660 --> 00:00:40,920 time. I started participating in Capture the Flag in about 2003, and along with the rest 7 00:00:40,920 --> 00:00:47,240 of Team Last Place, we won CTF in 2006 and 2007, so that was quite a bit of fun. But 8 00:00:47,240 --> 00:00:54,340 since we played CTF so much, I rarely actually went out to go see any talks or try to give 9 00:00:54,340 --> 00:00:56,380 any talks since we were doing CTF the whole time. 10 00:00:57,200 --> 00:01:02,660 But I still play in quals, because that's fun. I'm a member of the Ubuntu security team. 11 00:01:02,860 --> 00:01:08,760 I started that in 2006. So my background is in trying to get compiler hardening in better 12 00:01:08,760 --> 00:01:12,720 shape for Ubuntu, and now I'm looking a little bit more at kernel hardening. 13 00:01:15,580 --> 00:01:25,020 So a quick overview of Linux kernel exploitation. The main goal for any kind of kernel attack 14 00:01:25,020 --> 00:01:26,100 is to get... 15 00:01:26,100 --> 00:01:26,580 ... 16 00:01:26,600 --> 00:01:34,540 ...control over execution flow. Usually, you can just set the user ID of the process 17 00:01:34,540 --> 00:01:40,680 you're using to zero in your root, and you have full control over the system. But actually 18 00:01:40,680 --> 00:01:45,540 finding targets to do this is what's tricky. You need an arbitrary write to start with, 19 00:01:45,720 --> 00:01:48,520 and then going from there is what makes things more fun. 20 00:01:52,400 --> 00:01:56,220 So with an arbitrary write, once you've got that, the way to 21 00:01:56,220 --> 00:02:02,640 change program flow is to change function pointers. So you can aim the normal operation 22 00:02:02,640 --> 00:02:07,720 of the kernel into something that you have written as the attacker. Function tables are 23 00:02:07,720 --> 00:02:11,600 great for that, because they're just lists of functions that if you can find a way to 24 00:02:11,600 --> 00:02:16,480 trigger that function, then you have managed to trick the kernel into running your code. 25 00:02:18,200 --> 00:02:23,800 One of the first ones that I played with a bit was security ops, since I was familiar 26 00:02:23,800 --> 00:02:26,200 with the Linux security module interface. I was familiar with the Linux security module interface. 27 00:02:27,580 --> 00:02:33,000 That's basically for SELinux, AppArmor, other LSMs use the security ops function table 28 00:02:33,000 --> 00:02:38,700 for high-level actions. So I like replacing the ptrace access check because it was up 29 00:02:38,700 --> 00:02:42,400 the top. It didn't require too much cleanup after you used it. There's plenty of other 30 00:02:42,400 --> 00:02:47,060 things. You could use the IDT. There's a good Frack article on that. There's plenty 31 00:02:47,060 --> 00:02:47,920 of stuff to read on that. 32 00:02:49,600 --> 00:02:55,200 But my favorite at this point is now using a single SOC struct. So if you can create 33 00:02:55,200 --> 00:02:56,180 a socket, you can use a single SOC struct. So if you can create a socket, you can use 34 00:02:56,180 --> 00:03:01,260 a single SOC struct. You've got the destructor when the socket goes away that gets called. 35 00:03:01,500 --> 00:03:05,680 And if you can overwrite that function when you close the specific socket, it will actually 36 00:03:05,680 --> 00:03:10,920 run the code that you wanted it to run. And it's very easy to find that function because 37 00:03:10,920 --> 00:03:13,020 it's exported by the kernel for you to see. 38 00:03:15,020 --> 00:03:18,880 But all that's unimportant if you don't have a flaw. Obviously, you need that arbitrary 39 00:03:18,880 --> 00:03:25,780 right to begin with. So I started looking at interface boundaries, you know, where things 40 00:03:26,180 --> 00:03:32,340 make transitions. And I was especially interested in copy from user because that's where you're 41 00:03:32,340 --> 00:03:38,500 pulling information from user space and putting it somewhere in the kernel. So anything that 42 00:03:38,500 --> 00:03:41,400 goes wrong in that is going to be a nice thing. 43 00:03:41,620 --> 00:03:49,160 So there are a lot of callers, about 4,000. So it was a bit daunting to just start with 44 00:03:49,160 --> 00:03:54,020 that. So I'm looking for things that length isn't checked, source isn't checked, destination 45 00:03:54,020 --> 00:03:54,940 isn't checked, all that. 46 00:03:54,940 --> 00:04:02,100 We don't really need advanced static analysis. GREP will work most of the time. In fact, there's 47 00:04:02,100 --> 00:04:06,520 a version of copy from user that does not perform access checks at all. There were very, 48 00:04:06,580 --> 00:04:11,760 very few callers of that. And one of them was in Intel DRM. Another one was in RDS. 49 00:04:13,500 --> 00:04:20,660 But with just standard copy from user, I thought that I should go a little bit beyond standard 50 00:04:20,660 --> 00:04:21,440 regular expressions. 51 00:04:22,800 --> 00:04:24,920 So I started looking at a tool called copy from user. I thought that I should go a little 52 00:04:24,940 --> 00:04:26,800 bit beyond standard expressions. So I started looking at a tool called CooxNL, which is 53 00:04:26,800 --> 00:04:31,200 traditionally used for semantic patching. You want to patch a behavior that's going 54 00:04:31,200 --> 00:04:36,720 on in a source tree, replace something about a structure. It was much easier to use that. 55 00:04:36,820 --> 00:04:39,860 But I was really using it for semantic grep, and I'll give you a quick example. 56 00:04:42,700 --> 00:04:48,420 The first thing I would do is basically you define a position. So the copy from user is 57 00:04:48,420 --> 00:04:54,380 at position P is the first top of the top of that. And then a whole extensive list of 58 00:04:54,940 --> 00:05:02,140 things. So things that are verifiably correct. So an example whitelist pattern here is copy 59 00:05:02,140 --> 00:05:07,600 from user into the address of a structure that we've actually done a size of on. So 60 00:05:07,600 --> 00:05:11,920 that's not overflowed. We know it's the right size. So you can create a huge list of white 61 00:05:11,920 --> 00:05:18,100 of good patterns that make sense and you don't need to pay attention to. And then the final 62 00:05:18,620 --> 00:05:24,260 chunk of the semantic grep is just to if you have a copy from user and it is not at 63 00:05:24,260 --> 00:05:24,920 the same position, you can create a copy from user that's not at the same position. 64 00:05:24,940 --> 00:05:25,880 So if you have a copy from user and it is not at the same position as any of the whitelists, 65 00:05:25,940 --> 00:05:32,580 then you have something that actually needs analysis. And that let me look at a bunch 66 00:05:32,580 --> 00:05:38,360 of different things. So I tried to focus on areas that didn't have a lot of usage 67 00:05:38,360 --> 00:05:45,440 or users. There have been plenty of vulnerabilities in rare network protocols or interfaces of 68 00:05:45,440 --> 00:05:52,960 not a lot of consumers, especially compat. So when you're dealing with running 32-bit 69 00:05:52,960 --> 00:05:54,920 on a 64-bit or you have different interfaces, you're dealing with a lot of interfaces that 70 00:05:54,920 --> 00:05:57,260 are not as good. So if you have a lot of issues, you can go to the API versions like the Linux 71 00:05:57,260 --> 00:06:00,860 for video stuff. There's a lot of issues. In fact, the issues are so bad that you have 72 00:06:01,480 --> 00:06:10,880 severe problems come back. The CVE that I mentioned, CVE 2007-4573 and 2010-3301, 73 00:06:11,020 --> 00:06:17,720 it's the same issue. They just reintroduced it three years later. So what I found was 74 00:06:17,720 --> 00:06:23,040 actually a compat interface that had no users because the code could not possibly have ever 75 00:06:23,040 --> 00:06:23,940 worked the first time. 76 00:06:26,220 --> 00:06:30,820 So I apologize for dense code. Hopefully you can read some of this. But this was in the 77 00:06:30,820 --> 00:06:36,640 V4L compat issues. And the first top bold piece was a copy from user that immediately 78 00:06:36,640 --> 00:06:44,700 jumped out on the semantic patch because KP data, which is the target, and UP data size 79 00:06:44,700 --> 00:06:49,920 have no relationship at all that's obvious. And as I went back through it, I realized that 80 00:06:49,920 --> 00:06:53,920 KP, this pointer in KP was not validated anywhere. So I was really worried about that. And I 81 00:06:53,940 --> 00:06:59,980 In fact, it wasn't even filled in anywhere, it came from a local stack area of memory 82 00:07:00,920 --> 00:07:07,080 and there was no way to control KP data, so if you ever called this IO control, you would 83 00:07:07,080 --> 00:07:10,140 crash your system because you would be writing crap into who knows where. 84 00:07:11,840 --> 00:07:19,340 But I got to thinking, you know, it's uninitialized, which really means that it was initialized, 85 00:07:19,380 --> 00:07:20,940 but you just didn't know from what. 86 00:07:20,940 --> 00:07:26,140 So if you can control the execution environment leading up to that, then it is initialized 87 00:07:26,140 --> 00:07:28,220 because you put something there ahead of time. 88 00:07:30,060 --> 00:07:35,500 So one of the questions is why does this even show up, why didn't anyone notice it, and 89 00:07:35,500 --> 00:07:40,880 the problem is that we passed everything by pointer value, so the compiler just ignored 90 00:07:40,880 --> 00:07:43,480 it, figured we knew what we were doing when we built it. 91 00:07:43,960 --> 00:07:50,600 So to control this variable, you find something else that builds the stack in a similar fashion. 92 00:07:51,360 --> 00:07:55,540 Lucky for me, this was in an IO control, which means that you just pick a different 93 00:07:55,540 --> 00:07:59,640 option for your IO control and you'll actually have exactly the same stack layout. 94 00:08:00,020 --> 00:08:03,400 And in this case, they had a union of options. 95 00:08:03,620 --> 00:08:06,540 It was like it was made to be overlapped. 96 00:08:06,760 --> 00:08:12,520 So there's this other function, get video tuner 32, that would copy everything from 97 00:08:12,520 --> 00:08:17,400 user space into this union on the stack and do some other stuff in return. 98 00:08:17,900 --> 00:08:20,580 And that would actually leave us with the stack in the stack. 99 00:08:20,600 --> 00:08:21,700 In the state that we had left it. 100 00:08:23,780 --> 00:08:27,380 So you had to take a look at the two different structures that are there. 101 00:08:29,600 --> 00:08:31,800 Our target here is the video code 32. 102 00:08:31,980 --> 00:08:32,680 We want data. 103 00:08:32,820 --> 00:08:34,020 We want to be able to control data. 104 00:08:34,220 --> 00:08:38,560 So we have to figure out where it is in relation to the other structure in the union. 105 00:08:39,780 --> 00:08:46,240 And it was relatively straightforward because in video tuner, there was this large name 106 00:08:46,240 --> 00:08:47,060 string. 107 00:08:47,900 --> 00:08:50,420 So looking at it more graphically. 108 00:08:50,420 --> 00:08:53,560 There's all the crap before the IO control on the stack. 109 00:08:53,920 --> 00:09:01,620 And then after we've returned from the tuner IO control, what's left on the stack is whatever 110 00:09:01,620 --> 00:09:03,220 we had put in that structure. 111 00:09:04,000 --> 00:09:06,520 And then we can go and call the microcode function. 112 00:09:06,820 --> 00:09:11,020 And whatever was there will end up inside the data field. 113 00:09:12,360 --> 00:09:16,460 And there are no special tricks for data size and for the length and source that we're doing. 114 00:09:17,120 --> 00:09:18,740 The only thing we need to do is calculate. 115 00:09:19,260 --> 00:09:20,340 And that's the C. 116 00:09:20,420 --> 00:09:25,200 For just quickly calculating where we are offset inside the other structure. 117 00:09:28,800 --> 00:09:35,080 Now the one problem you run into is that you have to call these IO controls in order. 118 00:09:35,240 --> 00:09:39,220 Because if you do anything else between the two IO controls, you've just used the stack 119 00:09:39,220 --> 00:09:39,900 for something else. 120 00:09:40,160 --> 00:09:44,520 So you can't use any syscall wrappers like libsys syscall. 121 00:09:44,680 --> 00:09:47,660 You can't actually use that because it's going to go off and do other things with your stack. 122 00:09:48,400 --> 00:09:50,160 You have to actually call it. 123 00:09:50,420 --> 00:09:56,280 You can't aid in debugging by sticking a printf between the two because of course you're making 124 00:09:56,280 --> 00:09:57,460 yet another syscall in between. 125 00:09:57,800 --> 00:10:00,400 So you have to be very careful about that. 126 00:10:00,520 --> 00:10:05,600 Additionally, the kernel may go off and do other work between the two on behalf of your 127 00:10:05,600 --> 00:10:07,560 process if it's a new code path. 128 00:10:08,000 --> 00:10:13,460 So the solution for that was just to call the IO control a whole bunch of times in a 129 00:10:13,460 --> 00:10:16,920 row first so that it finished any work it had to do with page tables, got everything 130 00:10:16,920 --> 00:10:19,180 in line for you to then call the final one. 131 00:10:20,420 --> 00:10:25,320 And another small challenge is these are all compat syscalls so they're in 32-bit but we're 132 00:10:25,320 --> 00:10:27,260 actually trying to attack a 64-bit host. 133 00:10:27,560 --> 00:10:32,840 So we have to call a 32-bit syscall from a 64-bit executable which is just some more 134 00:10:32,840 --> 00:10:35,180 assembly which I have here. 135 00:10:35,280 --> 00:10:41,460 Basically the int 80 will force a 32-bit syscall so this is a quick C wrapper to shove the 136 00:10:41,460 --> 00:10:46,000 arguments I need into a 32-bit syscall and return. 137 00:10:48,000 --> 00:10:50,300 And then the action is to. 138 00:10:50,420 --> 00:10:53,500 You beat on it with the one that will prime the stack. 139 00:10:54,180 --> 00:10:57,460 So call it a bunch of times first, the tuner IO control. 140 00:10:58,200 --> 00:11:02,840 And then go after it with the buggy code since we know what's in memory now and we can 141 00:11:02,840 --> 00:11:04,600 actually reliably write stuff. 142 00:11:05,520 --> 00:11:08,020 But that just gets us an overwrite. 143 00:11:08,220 --> 00:11:09,460 How do we actually control it? 144 00:11:10,620 --> 00:11:17,600 I used the SOCKSTRUCT exploit from Dan Rosenberg's code recently and you basically open up a 145 00:11:17,600 --> 00:11:20,400 socket and it shows up in ProcNet. 146 00:11:20,400 --> 00:11:26,980 In bold is the first is the port that I'm listening on in hex and the other bold value 147 00:11:26,980 --> 00:11:30,160 is the actual kernel location of the socket that you need to target. 148 00:11:30,420 --> 00:11:37,020 So you just ask the kernel nicely for what you want to over flow or overwrite rather. 149 00:11:39,800 --> 00:11:44,920 In modern versions of the kernel, K pointer restrict now blocks ProcNet TCP but you can 150 00:11:44,920 --> 00:11:48,200 still get the same information out of a net link thing that is still open. 151 00:11:48,760 --> 00:11:50,320 So you just have to modify your code. 152 00:11:50,400 --> 00:11:50,680 A little bit. 153 00:11:51,700 --> 00:11:54,700 So then we're going to create a payload that we want the kernel to run. 154 00:11:55,220 --> 00:12:00,660 This is based on Brad Spangler's enlightenment code where you basically just create new credentials 155 00:12:00,660 --> 00:12:03,380 as root and commit them to the local process. 156 00:12:03,580 --> 00:12:05,500 You're just setting your UID to zero. 157 00:12:05,620 --> 00:12:10,900 This is the entire function that the kernel needs to run to give you root as that process. 158 00:12:11,700 --> 00:12:16,440 And finally, after you perform the overwrite, you just close the socket and it will call 159 00:12:16,440 --> 00:12:18,280 that function for you and everything is nice. 160 00:12:18,340 --> 00:12:19,360 The kernel cleans up for you. 161 00:12:20,700 --> 00:12:24,300 And I can show you a small demo of this running. 162 00:12:26,220 --> 00:12:27,240 If I can see things. 163 00:12:30,760 --> 00:12:31,900 Hopefully everyone can see that. 164 00:12:36,460 --> 00:12:37,400 And it flipped back. 165 00:12:42,780 --> 00:12:43,760 It's not going to let me. 166 00:12:47,480 --> 00:12:48,240 How about that? 167 00:12:48,440 --> 00:12:48,820 Okay. 168 00:12:48,820 --> 00:12:59,180 So a lot of demos sort of aren't very interesting because they effectively just show you a prompt. 169 00:12:59,500 --> 00:13:00,820 That's not very useful. 170 00:13:00,980 --> 00:13:01,880 That doesn't prove much. 171 00:13:02,060 --> 00:13:02,900 And it's not very exciting. 172 00:13:03,000 --> 00:13:03,720 It's not very dramatic. 173 00:13:04,540 --> 00:13:11,640 So I decided I ought to have this demo with a useless Hollywood drama mode. 174 00:13:13,740 --> 00:13:18,620 So you can ‑‑ it can be exciting and we can be on the edge of our seats waiting for 175 00:13:18,620 --> 00:13:19,380 something to happen. 176 00:13:22,940 --> 00:13:27,000 So this is resolving the credentials or the cred calls. 177 00:13:27,760 --> 00:13:31,940 And we have to find where we are in memory, open the socket and figure out where it is 178 00:13:31,940 --> 00:13:32,300 in memory. 179 00:13:33,080 --> 00:13:34,160 Open the video device. 180 00:13:34,400 --> 00:13:35,080 This is very exciting. 181 00:13:35,880 --> 00:13:36,860 Do the I.O. control. 182 00:13:37,640 --> 00:13:38,480 Did it work? 183 00:13:42,740 --> 00:13:43,500 It did. 184 00:13:43,680 --> 00:13:44,540 It did work. 185 00:13:48,080 --> 00:13:48,600 It did. 186 00:13:54,700 --> 00:13:55,140 We got it. 187 00:13:55,140 --> 00:13:57,840 And if you don't want to wait for it, just run it without the dash dash Hollywood option. 188 00:13:59,600 --> 00:14:00,820 Just run straight through. 189 00:14:05,160 --> 00:14:07,540 And that's basically it. 190 00:14:08,880 --> 00:14:12,820 I can go all the way through all my slides again and get to the end that says questions. 191 00:14:16,580 --> 00:14:17,400 And that's it.