-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevil_signal_fork.cpp
85 lines (72 loc) · 1.97 KB
/
evil_signal_fork.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <unistd.h>
#include <setjmp.h>
#include <execinfo.h>
void DumpCallstackPrintf() {
void *callstack[128];
int framesC = backtrace(callstack, sizeof(callstack));
printf("backtrace() returned %d addresses\n", framesC);
char** strs = backtrace_symbols(callstack, framesC);
for(int i = 0; i < framesC; ++i) {
if(strs[i])
printf("%s\n", strs[i]);
else
break;
}
free(strs);
}
sigjmp_buf longJumpBuffer;
void SimpleSignalHandler(int Sig) {
printf("SignalHandler: %i\n", Sig);
signal(Sig, SIG_IGN); // discard all remaining signals
DumpCallstackPrintf();
if(!fork()) {
abort();
}
signal(Sig, SimpleSignalHandler); // reset handler
printf("resuming ...\n");
fflush(stdout);
siglongjmp(longJumpBuffer, 1); // jump back to main loop, maybe we'll be able to continue somehow
}
int main() {
printf("installing signal handler ...\n");
signal(SIGSEGV, &SimpleSignalHandler);
signal(SIGTRAP, &SimpleSignalHandler);
signal(SIGABRT, &SimpleSignalHandler);
signal(SIGHUP, &SimpleSignalHandler);
signal(SIGBUS, &SimpleSignalHandler);
signal(SIGILL, &SimpleSignalHandler);
signal(SIGFPE, &SimpleSignalHandler);
signal(SIGSYS, &SimpleSignalHandler);
printf("callstack:\n");
DumpCallstackPrintf(); // dummy call to force loading dynamic lib at this point (with sane heap) for backtrace and friends
printf("entering main loop ...\n");
bool quit = false;
while(!quit) {
sigsetjmp(longJumpBuffer, 1);
using namespace std;
cout << "command: " << flush;
std::string cmd;
cin >> cmd;
if(cmd == "quit") quit = true;
else if(cmd == "crash") {
cout << "crashing ..." << endl;
(*(int*)0x13) = 42;
assert(false);
} else if(cmd == "evil") {
cout << "CRASH" << endl;
sigsetjmp(longJumpBuffer, 1);
assert(false);
} else
cout << "unknown cmd" << endl;
}
return 0;
}