-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsnapexec.c
589 lines (469 loc) · 13.9 KB
/
psnapexec.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#include <asm/prctl.h>
#include <assert.h>
#include <elf.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <fcntl.h>
#include "psnap.h"
#include "report.h"
#define PAGE_UP(x) (((unsigned long) x + 4095) & (~4095))
#define PAGE_DOWN(x) ((unsigned long) x & (~4095))
static struct snapshot*
parse_snapshot(void* elf)
{
struct snapshot* snap = malloc(sizeof(struct snapshot));
Elf_Ehdr* ehdr = (Elf_Ehdr*) elf;
Elf_Shdr* shdr = (Elf_Shdr*) (elf + ehdr->e_shoff);
for (size_t i = 0; i < ehdr->e_shnum; i++) {
switch (shdr[i].sh_type)
{
case SHT_PSNAP_REGS:
report("Found SHT_PSNAP_REGS section: offset=0x%08lx size=%lu",
shdr[i].sh_offset, shdr[i].sh_size);
snap->regs = (struct user_regs_struct*) (elf + shdr[i].sh_offset);
break;
case SHT_PSNAP_FPREGS:
report("Found SHT_PSNAP_FPREGS section: offset=0x%08lx size=%lu",
shdr[i].sh_offset, shdr[i].sh_size);
snap->fpregs = (struct user_fpregs_struct*) (elf + shdr[i].sh_offset);
break;
case SHT_PSNAP_FDINFO:
report("Found SHT_PSNAP_FPREGS section: offset=0x%08lx size=%lu",
shdr[i].sh_offset, shdr[i].sh_size);
snap->fdinfo = (struct fdinfo*) (elf + shdr[i].sh_offset);
snap->nfdinfo = shdr[i].sh_size / shdr[i].sh_entsize;
break;
default:
break;
}
}
return snap;
}
static int
open_file_handlers(struct fdinfo* fdinfo, size_t nfdinfo)
{
for (size_t i = 0; i < nfdinfo; i++) {
int fd = 0;
if (fdinfo[i].fd < 3)
continue;
if (strstr(fdinfo[i].path, "socket") ||
strstr(fdinfo[i].path, "anon_inode") ||
strstr(fdinfo[i].path, "pipe")) {
fd = open("/dev/null", fdinfo[i].flags);
} else {
fd = open(fdinfo[i].path, fdinfo[i].flags);
}
if (fd < 0) {
report_error("can't open '%s': %s", fdinfo[i].path,
strerror(errno));
return -1;
}
if (fdinfo[i].pos != 0 && lseek(fd, fdinfo[i].pos, SEEK_SET) < 0) {
report_error("can't seek in '%s': %s", fdinfo[i].path,
strerror(errno));
close(fd);
return -1;
}
if (dup2(fd, fdinfo[i].fd) < 0) {
report_error("can't set file descriptor %d for '%s'", fdinfo[i].fd,
fdinfo[i].path);
return -1;
}
report("Opened file handler: path=%s pos=%lu", fdinfo[i].path,
fdinfo[i].pos);
}
return 0;
}
static uint8_t*
generate_munmap(uint8_t* ptr, size_t addr, size_t len)
{
*ptr++ = 0x48;
*ptr++ = 0xc7;
*ptr++ = 0xc0;
*(uint32_t*) ptr = (uint32_t) 0x0b;
ptr += sizeof(uint32_t);
*ptr++ = 0x48;
*ptr++ = 0xbf;
*(uint64_t*) ptr = (uint64_t) addr;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xbe;
*(uint64_t*) ptr = (uint64_t) len;
ptr += sizeof(uint64_t);
*ptr++ = 0x0f;
*ptr++ = 0x05;
return ptr;
}
static uint8_t*
generate_munmap_current_executable(uint8_t* ptr)
{
uint8_t* ret = NULL;
FILE* stat_file = NULL;
char stat_path[64] = { 0 };
FILE* maps_file = NULL;
char maps_path[64] = { 0 };
size_t bottom = 0;
size_t top = 0;
char perms[5] = { 0 };
char buf[256] = { 0 };
char exe[256] = { 0 };
snprintf(stat_path, 64, "/proc/self/stat");
stat_file = fopen(stat_path, "r");
if (stat_file == NULL) {
report_error("can't open '%s': %s", stat_path, strerror(errno));
goto cleanup;
}
if (fscanf(stat_file, "%*d %s", exe) == EOF) {
report_error("can't read '%s': %s", stat_path, strerror(errno));
goto cleanup;
}
exe[strlen(exe) - 1] = '\0';
snprintf(maps_path, 64, "/proc/self/maps");
maps_file = fopen(maps_path, "r");
if (maps_file == NULL) {
report_error("can't open '%s': %s", maps_path, strerror(errno));
goto cleanup;
}
while (fscanf(maps_file, "%lx-%lx %s %[^\n]\n", &bottom, &top, perms,
buf) != EOF) {
if (strstr(buf, exe + 1) == NULL)
continue;
ptr = generate_munmap(ptr, bottom, top - bottom);
report("Unmapped region: base=0x%08lx size=%lu", bottom, top - bottom);
}
if (ferror(maps_file)) {
report_error("can't read '%s': %s", maps_path, strerror(errno));
goto cleanup;
}
ret = ptr;
cleanup:
if (stat_file != NULL)
fclose(stat_file);
if (maps_file != NULL)
fclose(maps_file);
return ret;
}
static uint8_t*
generate_mmap(uint8_t* ptr, size_t addr, size_t len, int prot, int flags)
{
*ptr++ = 0x48;
*ptr++ = 0xc7;
*ptr++ = 0xc0;
*(uint32_t*) ptr = (uint32_t) 0x09;
ptr += sizeof(uint32_t);
*ptr++ = 0x48;
*ptr++ = 0xbf;
*(uint64_t*) ptr = (uint64_t) addr;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xbe;
*(uint64_t*) ptr = (uint64_t) len;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xc7;
*ptr++ = 0xc2;
*(uint32_t*) ptr = (uint32_t) prot;
ptr += sizeof(uint32_t);
*ptr++ = 0x49;
*ptr++ = 0xc7;
*ptr++ = 0xc2;
*(uint32_t*) ptr = (uint32_t) flags;
ptr += sizeof(uint32_t);
*ptr++ = 0x49;
*ptr++ = 0xc7;
*ptr++ = 0xc0;
*(uint32_t*) ptr = (uint32_t) 0xffff;
ptr += sizeof(uint32_t);
*ptr++ = 0x49;
*ptr++ = 0xc7;
*ptr++ = 0xc1;
*(uint32_t*) ptr = (uint32_t) 0;
ptr += sizeof(uint32_t);
*ptr++ = 0x0f;
*ptr++ = 0x05;
return ptr;
}
const size_t mmap_size = 21 + 5 * sizeof(uint32_t) + 2 * sizeof(uint64_t);
static uint8_t*
generate_memcpy(uint8_t* ptr, size_t dest, size_t src, size_t len)
{
*ptr++ = 0x48;
*ptr++ = 0xbe;
*(uint64_t*) ptr = (uint64_t) src;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xbf;
*(uint64_t*) ptr = (uint64_t) dest;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xb9;
*(uint64_t*) ptr = (uint64_t) len;
ptr += sizeof(uint64_t);
*ptr++ = 0xf3;
*ptr++ = 0xa4;
return ptr;
}
const size_t memcpy_size = 8 + 3 * sizeof(uint64_t);
static uint8_t*
generate_mprotect(uint8_t* ptr, size_t addr, size_t len, int prot)
{
*ptr++ = 0x48;
*ptr++ = 0xc7;
*ptr++ = 0xc0;
*(uint32_t*) ptr = (uint32_t) 0x0a;
ptr += sizeof(uint32_t);
*ptr++ = 0x48;
*ptr++ = 0xbf;
*(uint64_t*) ptr = (uint64_t) addr;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xbe;
*(uint64_t*) ptr = (uint64_t) len;
ptr += sizeof(uint64_t);
*ptr++ = 0x48;
*ptr++ = 0xc7;
*ptr++ = 0xc2;
*(uint32_t*) ptr = (uint32_t) prot;
ptr += sizeof(uint32_t);
*ptr++ = 0x0f;
*ptr++ = 0x05;
return ptr;
}
const size_t mprotect_size = 12 + 2 * sizeof(uint32_t) + 2 * sizeof(uint64_t);
static void*
generate_jumpbuf(void* elf, struct snapshot* snap)
{
void* jumpbuf = NULL;
uint8_t* tmp = NULL;
Elf_Ehdr* ehdr = (Elf_Ehdr*) elf;
Elf_Phdr* phdr = (Elf_Phdr*) (elf + ehdr->e_phoff);
size_t per_phdr_size = mmap_size + memcpy_size + mprotect_size;
jumpbuf = mmap((void*) 0x1000000,
PAGE_UP(PAGE_SIZE + per_phdr_size * ehdr->e_phnum),
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (jumpbuf == MAP_FAILED) {
perror("mmap");
return NULL;
}
tmp = (uint8_t*) jumpbuf;
if ((tmp = generate_munmap_current_executable(tmp)) == NULL)
return NULL;
for (size_t i = 0; i < ehdr->e_phnum; i++) {
int prot = 0;
size_t load_addr = 0;
size_t len = 0;
if (phdr[i].p_type != PT_LOAD)
continue;
if (phdr[i].p_vaddr == 0xffffffffff600000)
continue;
load_addr = PAGE_DOWN(phdr[i].p_vaddr);
len = PAGE_UP(phdr[i].p_memsz + (phdr[i].p_vaddr % PAGE_SIZE));
tmp = generate_mmap(tmp, load_addr, len,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS);
tmp = generate_memcpy(tmp, phdr[i].p_vaddr,
(size_t) (elf + phdr[i].p_offset),
phdr[i].p_filesz);
if (phdr[i].p_flags & PF_R)
prot |= PROT_READ;
if (phdr[i].p_flags & PF_W)
prot |= PROT_WRITE;
if (phdr[i].p_flags & PF_X)
prot |= PROT_EXEC;
tmp = generate_mprotect(tmp, load_addr, len, prot);
report("Mapped segment: addr=0x%08lx len=%lu prot=%d flags=%d",
phdr[i].p_vaddr, phdr[i].p_memsz, prot,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS);
}
/*
* Recover fpregs
* 48 bf 00 b0 ff f7 ff movabs $0x7ffff7ffb000,%rdi
* 7f 00 00
* d9 27 fldenv (%rdi)
*/
*tmp++ = 0x48;
*tmp++ = 0xbf;
*(uint64_t*) tmp = (uint64_t) snap->fpregs;
tmp += sizeof(uint64_t);
*tmp++ = 0xd9;
*tmp++ = 0x27;
/*
* Recover stack pointer
* 48 bc d8 8b 91 48 fe movabs $0x7ffe48918bd8,%rsp
* 7f 00 00
*/
*tmp++ = 0x48;
*tmp++ = 0xbc;
*(uint64_t*) tmp = snap->regs->rsp;
tmp += sizeof(uint64_t);
/*
* Recover eflags/rflags
* 48 b8 02 02 01 00 00 movabs $0x10202,%rax
* 00 00 00
* 50 push %rax
* 9d popf
*/
*tmp++ = 0x48;
*tmp++ = 0xb8;
*(uint64_t*) tmp = (uint64_t) snap->regs->eflags;
tmp += sizeof(uint64_t);
*tmp++ = 0x50;
*tmp++ = 0x9d;
/*
* Store instruction pointer at top of the stack
* 48 b8 d2 7f 41 46 67 movabs $0x7f6746417fd2,%rax
* 7f 00 00
* 50 push %rax
*/
*tmp++ = 0x48;
*tmp++ = 0xb8;
*(uint64_t*) tmp = (uint64_t) snap->regs->rip;
tmp += sizeof(uint64_t);
*tmp++ = 0x50;
/*
* Recover fs base
* 48 b8 d2 7f 41 46 67 movabs $0x7f4e32296a7c,%rax
* 50 push %rax
* 48 c7 c0 9e 00 00 00 mov $0x9e,%rax
* 48 c7 c7 02 10 00 00 mov $0x1002,%rdi
* 48 be 40 1a c8 32 4e movabs $0x7f4e32c81a40,%rsi
* 7f 00 00
* syscall
*/
*tmp++ = 0x48;
*tmp++ = 0xc7;
*tmp++ = 0xc0;
*(uint32_t*) tmp = (uint32_t) 0x9e;
tmp += sizeof(uint32_t);
*tmp++ = 0x48;
*tmp++ = 0xc7;
*tmp++ = 0xc7;
*(uint32_t*) tmp = (uint32_t) ARCH_SET_FS;
tmp += sizeof(uint32_t);
*tmp++ = 0x48;
*tmp++ = 0xbe;
*(uint64_t*) tmp = (uint64_t) snap->regs->fs;
tmp += sizeof(uint64_t);
*tmp++ = 0x0f;
*tmp++ = 0x05;
/*
* Recover regs
* 48 b8 00 fe ff ff ff movabs $0xfffffffffffffe00,%rax
* ff ff ff
*/
*tmp++ = 0x48;
*tmp++ = 0xb8;
*(uint64_t*) tmp = (uint64_t) snap->regs->rax;
tmp += sizeof(uint64_t);
*tmp++ = 0x48;
*tmp++ = 0xbb;
*(uint64_t*) tmp = (uint64_t) snap->regs->rbx;
tmp += sizeof(uint64_t);
*tmp++ = 0x48;
*tmp++ = 0xb9;
*(uint64_t*) tmp = (uint64_t) snap->regs->rcx;
tmp += sizeof(uint64_t);
*tmp++ = 0x48;
*tmp++ = 0xba;
*(uint64_t*) tmp = (uint64_t) snap->regs->rdx;
tmp += sizeof(uint64_t);
*tmp++ = 0x48;
*tmp++ = 0xbd;
*(uint64_t*) tmp = (uint64_t) snap->regs->rbp;
tmp += sizeof(uint64_t);
*tmp++ = 0x48;
*tmp++ = 0xbe;
*(uint64_t*) tmp = (uint64_t) snap->regs->rsi;
tmp += sizeof(uint64_t);
*tmp++ = 0x48;
*tmp++ = 0xbf;
*(uint64_t*) tmp = (uint64_t) snap->regs->rdi;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xb8;
*(uint64_t*) tmp = (uint64_t) snap->regs->r8;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xb9;
*(uint64_t*) tmp = (uint64_t) snap->regs->r9;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xba;
*(uint64_t*) tmp = (uint64_t) snap->regs->r10;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xbb;
*(uint64_t*) tmp = (uint64_t) snap->regs->r11;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xbc;
*(uint64_t*) tmp = (uint64_t) snap->regs->r12;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xbd;
*(uint64_t*) tmp = (uint64_t) snap->regs->r13;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xbe;
*(uint64_t*) tmp = (uint64_t) snap->regs->r14;
tmp += sizeof(uint64_t);
*tmp++ = 0x49;
*tmp++ = 0xbf;
*(uint64_t*) tmp = (uint64_t) snap->regs->r15;
tmp += sizeof(uint64_t);
/*
* Recover instruction pointer from top of the stack
* c3 ret
*/
*tmp++ = 0xc3;
report("Allocated jumpbuf: addr=0x%08lx size=%lu rip=0x%08lx rsp=0x%08lx",
(size_t) jumpbuf, (size_t) tmp - (size_t) jumpbuf, snap->regs->rip,
snap->regs->rsp);
return jumpbuf;
}
int
main(int argc, char* argv[])
{
int ret = EXIT_FAILURE;
int fd = -1;
void* elf = NULL;
struct stat st = { 0 };
struct snapshot* snap = NULL;
void (*jump)(void);
set_program_name(argv[0]);
fd = open(argv[1], O_RDONLY, S_IRWXU);
if (fd == -1) {
perror("open");
goto cleanup;
}
if (fstat(fd, &st) < 0) {
perror("fstat");
goto cleanup;
}
elf = mmap((void*) 0x3000000, st.st_size, PROT_READ,
MAP_PRIVATE | MAP_FIXED, fd, 0);
if (elf == MAP_FAILED) {
perror("mmap");
goto cleanup;
}
report("Allocated snapshot: addr=0x%lx size=%lu", (size_t) elf,
st.st_size);
snap = parse_snapshot(elf);
if (open_file_handlers(snap->fdinfo, snap->nfdinfo) < 0)
goto cleanup;
if ((jump = generate_jumpbuf(elf, snap)) == NULL)
goto cleanup;
jump();
ret = EXIT_SUCCESS;
cleanup:
munmap(elf, st.st_size);
return ret;
}