forked from kernelslacker/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-mortem.c
77 lines (64 loc) · 1.59 KB
/
post-mortem.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
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>
#include "log.h"
#include "pids.h"
#include "shm.h"
#include "taint.h"
#include "trinity.h"
#include "post-mortem.h"
#include "utils.h"
static void dump_syscall_rec(FILE *fd, struct syscallrecord *rec)
{
switch (rec->state) {
case UNKNOWN:
/* new child, so nothing to dump. */
break;
case PREP:
/* haven't finished setting up, so don't dump. */
break;
case BEFORE:
fprintf(fd, "%s\n", rec->prebuffer);
break;
case AFTER:
case DONE:
case GOING_AWAY:
fprintf(fd, "%s%s\n", rec->prebuffer, rec->postbuffer);
break;
}
}
static void dump_syscall_records(void)
{
FILE *fd;
unsigned int i;
fd = fopen("trinity-post-mortem.log", "w");
if (!fd) {
outputerr("Failed to write post mortem log (%s)\n", strerror(errno));
return;
}
for_each_child(i) {
dump_syscall_rec(fd, &shm->children[i]->previous);
dump_syscall_rec(fd, &shm->children[i]->syscall);
fprintf(fd, "\n");
}
fclose(fd);
}
void tainted_postmortem(int taint)
{
struct timeval taint_tv;
shm->postmortem_in_progress = TRUE;
//TODO: Sort syscall rec output by timeval, and mark when we detected taint_tv.
gettimeofday(&taint_tv, NULL);
panic(EXIT_KERNEL_TAINTED);
output(0, "kernel became tainted! (%d/%d) Last seed was %u\n",
taint, kernel_taint_initial, shm->seed);
openlog("trinity", LOG_CONS|LOG_PERROR, LOG_USER);
syslog(LOG_CRIT, "Detected kernel tainting. Last seed was %u\n", shm->seed);
closelog();
dump_syscall_records();
shm->postmortem_in_progress = FALSE;
}