-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoized.d
723 lines (631 loc) · 20.7 KB
/
memoized.d
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// #include <sys/ptrace.h>
// #include <bits/types.h>
// #include <sys/user.h>
// #include <sys/wait.h>
// #include <sys/types.h>
// #include <sys/stat.h>
// #include <fcntl.h>
// #include <syscall.h>
// C library
// #include <unistd.h>
// #include <cstdlib>
// #include <cstdio>
// #include <cerrno>
// #include <cassert>
// #include <cstring>
// #include <cinttypes>
// OpenSSL
// #include <openssl/sha.h>
// system calls
import syscalls;
import syscallsents;
/* cheap trick for reading syscall number / return value. */
#ifdef __amd64__
#define eax rax
#define orig_eax orig_rax
#else
#endif
#ifndef offsetof
#define offsetof(a, b) __builtin_offsetof(a,b)
#endif
#define getReg(child, name) __get_reg(child, offsetof(struct user, regs.name))
long __get_reg(pid_t child, int off)
{
long val = ptrace(PTRACE_PEEKUSER, child, off);
assert(errno == 0);
return val;
}
typedef unsigned long ulong;
typedef std::vector<std::string> Paths;
/// System calls executed.
typedef std::vector<std::string> DoneSyscalls[MAX_SYSCALL_NUM + 1];
/// Paths by pid.
typedef std::map<pid_t, Paths> PathsByPid;
/// Trace results.
struct Trace
{
DoneSyscalls doneSyscalls;
/// Read-only opened file paths by pid.
PathsByPid inPathsByPid;
/// Write-only opened file paths by pid.
PathsByPid outPathsByPid;
/// Stated file paths by pid.
PathsByPid statPathsByPid;
SHA256_CTX inputHash;
};
/** Wait for system call in `child`. */
int waitForChildPidSyscall(pid_t child)
{
while (true)
{
// tracee is stopped at the next entry to or exit from a system call
ptrace(PTRACE_SYSCALL, child, 0, 0);
int status;
waitpid(child, &status, 0); // wait for child
if (WIFSTOPPED(status) && // if child stopped and
WSTOPSIG(status) & 0x80) // highest bit set
{
return 0;
}
if (WIFEXITED(status)) // if child exited
{
return 1;
}
fprintf(stderr, "[stopped status:%d stopsignal:%x]\n", status, WSTOPSIG(status));
}
}
const char *syscallNameOfNumber(int syscallNumber)
{
if (syscallNumber <= MAX_SYSCALL_NUM)
{
struct syscall_entry *ent = &syscalls[syscallNumber];
if (ent->name)
{
return ent->name;
}
}
static char buf[128];
snprintf(buf, sizeof buf, "sys_%d", syscallNumber);
return buf;
}
ulong pidSyscallArg(pid_t child, int which)
{
switch (which)
{
#ifdef __amd64__
case 0: return getReg(child, rdi);
case 1: return getReg(child, rsi);
case 2: return getReg(child, rdx);
case 3: return getReg(child, r10);
case 4: return getReg(child, r8);
case 5: return getReg(child, r9);
#else
case 0: return getReg(child, ebx);
case 1: return getReg(child, ecx);
case 2: return getReg(child, edx);
case 3: return getReg(child, esi);
case 4: return getReg(child, edi);
case 5: return getReg(child, ebp);
#endif
default: return -1L;
}
}
/** Allocate and return a copy of a null-terminated C string at `addr`. */
char *readString(pid_t child, unsigned long addr)
{
uint allocated = 4096;
char *val = reinterpret_cast<char*>(malloc(allocated));
int read = 0;
unsigned long tmp;
while (true)
{
if (read + sizeof(tmp) > allocated)
{
allocated *= 2;
val = reinterpret_cast<char*>(realloc(val, allocated));
}
tmp = ptrace(PTRACE_PEEKDATA, child, addr + read);
if (errno != 0)
{
val[read] = 0;
break;
}
memcpy(val + read, &tmp, sizeof(tmp));
if (memchr(&tmp, 0, sizeof(tmp)) != NULL)
{
break;
}
read += sizeof(tmp);
}
return val;
}
/** Allocate and return a copy of a null-terminated C string at `addr`.
TODO templatize T on struct stat when moving to D
*/
struct stat readStat(pid_t child, unsigned long addr)
{
struct stat stat;
uint8_t* statPtr = reinterpret_cast<uint8_t*>(&stat); // TODO byte
int read = 0;
while (true)
{
unsigned long tmp = ptrace(PTRACE_PEEKDATA, child, addr + read);
if (errno != 0)
{
fprintf(stderr, "memoized: error: cannot read ptrace(PTRACE_PEEKDATA)\n");
break;
}
memcpy(statPtr + read, &tmp, sizeof(tmp));
if (memchr(&tmp, 0, sizeof(tmp)) != NULL)
{
break;
}
read += sizeof(tmp);
}
return stat;
}
void printSyscallArgs(pid_t child, int num)
{
const char * sname = syscallNameOfNumber(num);
struct syscall_entry *ent = NULL;
int nargs = SYSCALL_MAXARGS;
int i;
const bool show = false;
if (num <= MAX_SYSCALL_NUM && syscalls[num].name)
{
ent = &syscalls[num];
nargs = ent->nargs;
}
for (i = 0; i < nargs; i++) // incorrectly always 6
{
const ulong arg = pidSyscallArg(child, i);
const int type = ent ? ent->args[i] : Arg.PTR;
if (strcmp(sname, "execve") == 0)
{
if (i == 0)
{
char* strval = readString(child, arg);
if (show) fprintf(stderr, "\"%s\"", strval);
free(strval);
goto done;
}
}
else if (strcmp(sname, "open") == 0 ||
strcmp(sname, "utime") == 0 ||
strcmp(sname, "access") == 0 ||
strcmp(sname, "mount") == 0 ||
strcmp(sname, "link") == 0 ||
strcmp(sname, "unlink") == 0 ||
strcmp(sname, "mkdir") == 0 ||
strcmp(sname, "stat") == 0 ||
strcmp(sname, "lstat") == 0 ||
strcmp(sname, "statfs") == 0 ||
strcmp(sname, "creat") == 0 ||
strcmp(sname, "chdir") == 0 ||
strcmp(sname, "chown") == 0 ||
strcmp(sname, "lchown") == 0 ||
strcmp(sname, "chmod") == 0 ||
strcmp(sname, "mknod") == 0)
{
if (i == 0)
{
char* strval = readString(child, arg);
if (show) fprintf(stderr, "\"%s\"", strval);
free(strval);
goto done;
}
}
else if (strcmp(sname, "mount") == 0 ||
strcmp(sname, "link") == 0 ||
strcmp(sname, "mkdirat") == 0 ||
strcmp(sname, "openat") == 0 ||
strcmp(sname, "fchmodat") == 0 ||
strcmp(sname, "fstatat") == 0) // TODO calculate full path from dirfd and pathname
{
if (i == 1)
{
char* strval = readString(child, arg);
if (show) fprintf(stderr, "\"%s\"", strval);
free(strval);
goto done;
}
}
else if (strcmp(sname, "mount") == 0)
{
if (i == 2)
{
char* strval = readString(child, arg);
if (show) fprintf(stderr, "\"%s\"", strval);
free(strval);
goto done;
}
}
// generic printing
switch (type)
{
case Arg.INT:
{
if (show) fprintf(stderr, "%ld", arg);
break;
}
case Arg.STR:
{
char* strval = readString(child, arg);
if (show) fprintf(stderr, "\"%s\"", strval);
free(strval);
break;
}
default:
{
if (show) fprintf(stderr, "0x%lx", arg);
break;
}
}
done:
if (i != nargs - 1) /* if not parameter */
{
if (show) fprintf(stderr, ", ");
}
}
}
/// Print newline to `file`.
void endl(FILE* file = stderr)
{
fprintf(file, "\n");
}
void printSyscall(pid_t child, long syscall_num, long retval)
{
fprintf(stderr, "%d %s(", child, syscallNameOfNumber(syscall_num));
printSyscallArgs(child, syscall_num);
fprintf(stderr, ") = ");
fprintf(stderr, "%ld", retval);
}
void handleSyscall(pid_t child,
Trace& trace)
{
const bool show = false;
const long syscall_num = getReg(child, orig_eax);
assert(errno == 0);
// return value
long retval = getReg(child, eax);
assert(errno == 0);
if (retval == -38) // on call entry
{
// printSyscall(child, syscall_num, retval); endl(stderr);
// nothing useful to do here for now
}
else // on call return
{
if (retval >= 0) // on successful return
{
// file io successful return
if (syscall_num == SYS_open ||
syscall_num == SYS_openat ||
syscall_num == SYS_creat ||
syscall_num == SYS_stat ||
syscall_num == SYS_statfs ||
syscall_num == SYS_lstat ||
syscall_num == SYS_access) // file system syscalls with path as first argument
{
// TODO prevent allocation
char* const pathC = readString(child, pidSyscallArg(child, 0)); // TODO prevent allocation
std::string path = pathC;
free(pathC); // TODO prevent deallocation
trace.doneSyscalls[syscall_num].push_back(path);
if (show)
{
printSyscall(child, syscall_num, retval);
}
switch (syscall_num)
{
case SYS_stat:
case SYS_lstat: // TODO specialcase on lstat
{
const struct stat stat = readStat(child, pidSyscallArg(child, 1));
const time_t ctime = stat.st_ctime; // creation
const time_t mtime = stat.st_mtime; // modification
const time_t atime = stat.st_atime; // access
if (ctime)
{
if (show) { fprintf(stderr, " ctime:%lu", ctime); }
}
if (mtime)
{
if (show) { fprintf(stderr, " mtime:%lu", mtime); }
}
if (atime)
{
if (show) { fprintf(stderr, " atime:%lu", atime); }
}
break;
}
case SYS_open:
{
// decode open arguments
const auto flags = pidSyscallArg(child, 1);
// mode in case a new file is created
const auto mode = pidSyscallArg(child, 2);
// true if this open only reads from file
const bool read_flag = ((flags & (O_RDONLY)) ||
(flags & (O_CLOEXEC)) ||
(flags & (O_NOCTTY)) ||
(flags == 0));
// true if this open only writes to file
const bool write_flag = ((flags & (O_WRONLY)) ||
(flags & (O_RDWR |
O_CREAT |
O_TRUNC)) ||
(flags & (O_WRONLY |
O_CREAT |
O_TRUNC)));
// assure that we decoded correctly
assert(read_flag || write_flag);
if (read_flag)
{
trace.inPathsByPid[child].push_back(std::string(path));
}
if (write_flag)
{
trace.outPathsByPid[child].push_back(std::string(path));
}
if (show)
{
fprintf(stderr, " ------- flags:%lx, mode:%lx, flags:", flags, mode);
const bool verbose = true;
if (verbose)
{
// print flags
if (flags & O_RDONLY) { fprintf(stderr, " O_RDONLY"); }
if (flags & O_WRONLY) { fprintf(stderr, " O_WRONLY"); }
if (flags & O_RDWR) { fprintf(stderr, " O_RDWR"); }
if (flags & O_DIRECTORY) { fprintf(stderr, " O_DIRECTORY"); }
if (flags & O_CREAT) { fprintf(stderr, " O_CREAT"); }
if (flags & O_TRUNC) { fprintf(stderr, " O_TRUNC"); }
if (flags & O_APPEND) { fprintf(stderr, " O_APPEND"); }
if (flags & O_CLOEXEC) { fprintf(stderr, " O_CLOEXEC"); }
if (flags & O_DIRECT) { fprintf(stderr, " O_DIRECT"); }
if (flags & O_DSYNC) { fprintf(stderr, " O_DSYNC"); }
if (flags & O_EXCL) { fprintf(stderr, " O_EXCL"); }
if (flags & O_LARGEFILE) { fprintf(stderr, " O_LARGEFILE"); }
if (flags & O_NOATIME) { fprintf(stderr, " O_NOATIME"); }
if (flags & O_NOCTTY) { fprintf(stderr, " O_NOCTTY"); }
if (flags & O_NOFOLLOW) { fprintf(stderr, " O_NOFOLLOW"); }
if (read_flag) { fprintf(stderr, " READ"); }
if (write_flag) { fprintf(stderr, " WRITE"); }
}
}
break;
}
default:
break;
}
if (show)
{
endl();
}
}
else if (syscall_num == SYS_execve ||
syscall_num == SYS_vfork ||
syscall_num == SYS_clone)
{
// fprintf(stderr, "syscall_num:%d\n", syscall_num);
// printSyscall(child, syscall_num, retval);
// endl(stderr);
// endl(stderr);
}
else
{
// fprintf(stderr, "syscall_num:%d\n", syscall_num);
// printSyscall(child, syscall_num, retval);
// endl(stderr);
// endl(stderr);
}
const bool verbose = false;
if (verbose)
{
printSyscall(child, syscall_num, retval);
endl();
}
}
else // on failure return
{
}
}
}
int doChild(int argc, char **argv)
{
char *args [argc+1];
int i;
for (i = 0; i < argc; i++)
{
args[i] = argv[i];
}
args[argc] = NULL;
ptrace(PTRACE_TRACEME);
fprintf(stderr, "memoized: info: execvp:%s\n", args[0]);
kill(getpid(), SIGSTOP); // stop it directly
return execvp(args[0], args);
}
int ptraceTopChild(pid_t top_child, Trace& trace)
{
SHA256_Init(&trace.inputHash);
// int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
// int SHA256_Final(unsigned char *md, SHA256_CTX *c);
while (true)
{
int status;
const pid_t child = waitpid(-1, &status, __WALL); // wait for next child event
if (child < 0)
{
return -1; // failed
}
const int event = status >> 16;
switch (event)
{
case PTRACE_EVENT_CLONE:
case PTRACE_EVENT_FORK:
case PTRACE_EVENT_VFORK:
{
// get pid of sub-child
long newpid;
ptrace(PTRACE_GETEVENTMSG, child, NULL, (long) &newpid);
ptrace(PTRACE_SYSCALL, newpid, NULL, NULL);
fprintf(stderr, "memoized: info: attached to offspring %ld\n", newpid);
break;
}
default:
if (WIFEXITED(status))
{
fprintf(stderr, "memoized: info: child %d exited\n", child);
if (child == top_child)
{
return 0; // top child exited, we're done
}
}
else if (WIFSTOPPED(status)) // stopped
{
const long stopsig = WSTOPSIG(status);
if (stopsig & 0x80) // highest bit set
{
fprintf(stderr, "child:%d TODO handle highest bit set\n", child);
}
else // normal case
{
// fprintf(stderr, "child:%d stopped with signal %ld\n", child, stopsig);
handleSyscall(child, trace);
}
}
else
{
fprintf(stderr, "child:%d TODO handle other status:%d\n", child, status);
}
break;
}
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
}
}
void attachAndPtraceTopChild(pid_t top_child)
{
const long traceRetVal = ptrace(PTRACE_ATTACH, top_child, NULL, NULL);
if (traceRetVal == 0) // success
{
fprintf(stderr, "memoized: info: attached to %d\n",top_child);
}
else
{
fprintf(stderr, "error: attach failed with return code %ld\n", traceRetVal);
}
const long opt = (PTRACE_O_EXITKILL |
PTRACE_O_TRACEEXEC |
PTRACE_O_TRACEFORK |
PTRACE_O_TRACECLONE |
PTRACE_O_TRACEVFORK |
PTRACE_O_TRACEVFORKDONE);
ptrace(PTRACE_SETOPTIONS, top_child, NULL, opt);
Trace trace;
ptraceTopChild(top_child, trace);
}
int main(int argc, char **argv)
{
pid_t child;
int push = 1;
int syscall = -1;
if (argc < 2)
{
fprintf(stderr, "Usage: %s [-s <syscall int>|-n <syscall name>] <program> <args>\n", argv[0]);
exit(1);
}
if (strcmp(argv[1], "-s") == 0)
{
syscall = atoi(argv[2]);
if (syscall > MAX_SYSCALL_NUM || syscall < 0)
{
fprintf(stderr, "error: %s is an invalid syscall\n", argv[2]);
exit(1);
}
push = 3;
}
if (strcmp(argv[1], "-n") == 0)
{
char *syscallname = argv[2];
struct syscall_entry *ent;
uint i;
for (i = 0; i < sizeof(syscalls)/sizeof(*syscalls); i++)
{
ent = &syscalls[i];
if (strcmp(syscallname, ent->name) == 0)
{
syscall = i;
break;
}
}
if (syscall == -1)
{
fprintf(stderr, "error: %s is an invalid syscall\n", argv[2]);
exit(1);
}
push = 3;
}
child = fork();
if (child == -1) /* fork failed */
{
fprintf(stderr, "error: Failed to fork()\n");
exit(child);
}
if (child == 0) /* in the child */
{
return doChild(argc-push, argv+push);
}
else /* in the parent */
{
attachAndPtraceTopChild(child);
}
}
// int do_trace_top(pid_t root_child, int syscall_req)
// {
// int status;
// int retval;
// waitpid(root_child, &status, 0);
// assert(WIFSTOPPED(status));
// ptrace(PTRACE_SETOPTIONS, root_child, 0,
// PTRACE_O_TRACESYSGOOD |
// PTRACE_O_EXITKILL |
// PTRACE_O_TRACECLONE |
// PTRACE_O_TRACEEXEC |
// PTRACE_O_TRACEFORK |
// PTRACE_O_TRACEVFORK |
// PTRACE_O_TRACEVFORKDONE
// );
// const uint max_childs = 4096;
// uint child_count = 0;
// pid_t childs[max_childs];
// childs[0] = root_child;
// child_count = 1;
// while (true) /* TODO while children not empty */
// {
// for (uint childs_index = 0; childs_index != child_count; ++childs_index)
// {
// const pid_t child = childs[childs_index];
// if (false)
// fprintf(stderr, "child:%d\n", child);
// if (waitForChildPidSyscall(child) != 0)
// {
// goto done; /* TODO remove child from list of children */
// }
// /* pre syscall */
// handleSyscall(child, syscall_req);
// fprintf(stderr, "\n");
// if (waitForChildPidSyscall(child) != 0)
// {
// goto done; /* TODO remove child from list of children */
// }
// /* post syscall */
// handleSyscall(child, syscall_req);
// retval = getReg(child, eax);
// assert(errno == 0);
// fprintf(stderr, "%d\n", retval);
// }
// }
// done:
// return 0;
// }