-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
393 lines (314 loc) · 8.26 KB
/
main.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
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include "trinity.h"
#include "child.h"
#include "signals.h"
#include "shm.h"
#include "files.h"
#include "random.h"
#include "syscall.h"
#include "pids.h"
#include "log.h"
#include "params.h"
#include "maps.h"
static void regenerate(void)
{
if (no_files == TRUE) /* We don't regenerate sockets */
return;
shm->regenerating = TRUE;
sleep(1); /* give children time to finish with fds. */
shm->regenerate = 0;
output(0, "[%d] Regenerating random pages, fd's etc.\n", getpid());
regenerate_fds();
destroy_maps();
setup_maps();
generate_random_page(page_rand);
shm->regenerating = FALSE;
}
bool ignore_tainted;
int check_tainted(void)
{
int fd;
int ret;
char buffer[4];
fd = open("/proc/sys/kernel/tainted", O_RDONLY);
if (!fd)
return -1;
ret = read(fd, buffer, 3);
close(fd);
if (ret > 0)
ret = atoi(buffer);
else {
/* We should never fail, but if we do, assume untainted. */
ret = 0;
}
return ret;
}
static void oom_score_adj(int adj)
{
FILE *fp;
fp = fopen("/proc/self/oom_score_adj", "w");
if (!fp)
return;
fprintf(fp, "%d", adj);
fclose(fp);
}
#define debugf if (debug == TRUE) printf
static void fork_children(void)
{
int pidslot;
static char childname[17];
/* Generate children*/
while (shm->running_childs < shm->max_children) {
int pid = 0;
/* Find a space for it in the pid map */
pidslot = find_pid_slot(EMPTY_PIDSLOT);
if (pidslot == PIDSLOT_NOT_FOUND) {
printf("[%d] ## Pid map was full!\n", getpid());
dump_pid_slots();
exit(EXIT_FAILURE);
}
(void)alarm(0);
fflush(stdout);
pid = fork();
if (pid != 0)
shm->pids[pidslot] = pid;
else {
/* Child process. */
int ret = 0;
mask_signals_child();
memset(childname, 0, sizeof(childname));
sprintf(childname, "trinity-child%d", pidslot);
prctl(PR_SET_NAME, (unsigned long) &childname);
oom_score_adj(500);
/* Wait for parent to set our pidslot */
while (shm->pids[pidslot] != getpid()) {
/* Make sure parent is actually alive to wait for us. */
ret = pid_alive(shm->parentpid);
if (ret != 0) {
shm->exit_reason = EXIT_SHM_CORRUPTION;
printf("[%d] " BUGTXT "parent (%d) went away!\n", getpid(), shm->parentpid);
sleep(20000);
}
}
init_child(pidslot);
ret = child_process(pidslot);
output(1, "child %d exiting\n", getpid());
_exit(ret);
}
shm->running_childs++;
debugf("[%d] Created child %d in pidslot %d [total:%d/%d]\n",
getpid(), shm->pids[pidslot], pidslot,
shm->running_childs, shm->max_children);
if (shm->exit_reason != STILL_RUNNING)
return;
}
debugf("[%d] created enough children\n", getpid());
}
void reap_child(pid_t childpid)
{
int i;
while (shm->reaper_lock == LOCKED);
shm->reaper_lock = LOCKED;
if (childpid == shm->last_reaped) {
debugf("[%d] already reaped %d!\n", getpid(), childpid);
goto out;
}
i = find_pid_slot(childpid);
if (i == PIDSLOT_NOT_FOUND)
goto out;
debugf("[%d] Removing pid %d from pidmap.\n", getpid(), childpid);
shm->pids[i] = EMPTY_PIDSLOT;
shm->running_childs--;
shm->tv[i].tv_sec = 0;
shm->last_reaped = childpid;
out:
shm->reaper_lock = UNLOCKED;
}
static void handle_child(pid_t childpid, int childstatus)
{
unsigned int i;
int slot;
switch (childpid) {
case 0:
//debugf("[%d] Nothing changed. children:%d\n", getpid(), shm->running_childs);
break;
case -1:
if (shm->exit_reason != STILL_RUNNING)
return;
if (errno == ECHILD) {
debugf("[%d] All children exited!\n", getpid());
for_each_pidslot(i) {
if (shm->pids[i] != EMPTY_PIDSLOT) {
if (pid_alive(shm->pids[i]) == -1) {
debugf("[%d] Removing %d from pidmap\n", getpid(), shm->pids[i]);
shm->pids[i] = EMPTY_PIDSLOT;
shm->running_childs--;
} else {
debugf("[%d] %d looks still alive! ignoring.\n", getpid(), shm->pids[i]);
}
}
}
break;
}
output(0, "error! (%s)\n", strerror(errno));
break;
default:
debugf("[%d] Something happened to pid %d\n", getpid(), childpid);
if (WIFEXITED(childstatus)) {
slot = find_pid_slot(childpid);
if (slot == PIDSLOT_NOT_FOUND) {
printf("[%d] ## Couldn't find pid slot for %d\n", getpid(), childpid);
shm->exit_reason = EXIT_LOST_PID_SLOT;
dump_pid_slots();
} else {
debugf("[%d] Child %d exited after %ld syscalls.\n", getpid(), childpid, shm->child_syscall_count[slot]);
reap_child(childpid);
}
break;
} else if (WIFSIGNALED(childstatus)) {
switch (WTERMSIG(childstatus)) {
case SIGALRM:
debugf("[%d] got a alarm signal from pid %d\n", getpid(), childpid);
break;
case SIGFPE:
case SIGSEGV:
case SIGKILL:
case SIGPIPE:
case SIGABRT:
debugf("[%d] got a signal from pid %d (%s)\n", getpid(), childpid, strsignal(WTERMSIG(childstatus)));
reap_child(childpid);
break;
default:
debugf("[%d] ** Child got an unhandled signal (%d)\n", getpid(), WTERMSIG(childstatus));
break;
}
break;
} else if (WIFSTOPPED(childstatus)) {
switch (WSTOPSIG(childstatus)) {
case SIGALRM:
debugf("[%d] got an alarm signal from pid %d\n", getpid(), childpid);
break;
case SIGSTOP:
debugf("[%d] Sending PTRACE_DETACH (and then KILL)\n", getpid());
ptrace(PTRACE_DETACH, childpid, NULL, NULL);
kill(childpid, SIGKILL);
reap_child(childpid);
break;
case SIGFPE:
case SIGSEGV:
case SIGKILL:
case SIGPIPE:
case SIGABRT:
debugf("[%d] Child %d was stopped by %s\n", getpid(), childpid, strsignal(WTERMSIG(childstatus)));
reap_child(childpid);
break;
default:
debugf("[%d] Child %d was stopped by unhandled signal (%s).\n", getpid(), childpid, strsignal(WSTOPSIG(childstatus)));
break;
}
break;
} else if (WIFCONTINUED(childstatus)) {
break;
} else {
output(0, "erk, wtf\n");
}
}
}
static void handle_children(void)
{
unsigned int i;
int childstatus;
pid_t pid;
if (shm->running_childs == 0)
return;
/* First, we wait for *any* child to wake us up. */
pid = waitpid(-1, &childstatus, WUNTRACED | WCONTINUED);
/* We were awoken, handle it. */
handle_child(pid, childstatus);
/* While we're awake, let's see if the other children need attention.
* We do this instead of just waitpid(-1) again so that there's no way
* for any one child to starve the others of attention.
*/
for_each_pidslot(i) {
pid = shm->pids[i];
if (pid == EMPTY_PIDSLOT)
continue;
if (pid_is_valid(pid) == FALSE)
return;
pid = waitpid(pid, &childstatus, WUNTRACED | WCONTINUED | WNOHANG);
if (pid != 0)
handle_child(pid, childstatus);
}
}
static const char *reasons[] = {
"Still running",
"No more syscalls enabled",
"Reached maximum syscall count",
"No file descriptors open",
"Lost track of a pid slot",
"shm corruption - Found a pid out of range.",
"ctrl-c",
"kernel became tainted",
"SHM was corrupted!",
"Child reparenting problem",
"No files in file list",
};
static const char * decode_exit(unsigned int reason)
{
return reasons[reason];
}
static void main_loop(void)
{
while (shm->exit_reason == STILL_RUNNING) {
if (shm->running_childs < shm->max_children) {
reseed();
fork_children();
}
if (shm->regenerate >= REGENERATION_POINT)
regenerate();
if (shm->need_reseed == TRUE)
reseed();
handle_children();
}
}
void do_main_loop(void)
{
const char taskname[13]="trinity-main";
int childstatus;
pid_t pid;
/* do an extra fork so that the watchdog and the children don't share a common parent */
fflush(stdout);
pid = fork();
if (pid == 0) {
setup_main_signals();
shm->parentpid = getpid();
output(0, "[%d] Main thread is alive.\n", getpid());
prctl(PR_SET_NAME, (unsigned long) &taskname);
set_seed(0);
setup_fds();
if (no_files == FALSE) {
if (files_in_index == 0) {
shm->exit_reason = EXIT_NO_FILES;
_exit(EXIT_FAILURE);
}
}
main_loop();
/* Wait until all children have exited. */
while (pidmap_empty() == FALSE)
handle_children();
printf("[%d] Bailing main loop. Exit reason: %s\n", getpid(), decode_exit(shm->exit_reason));
_exit(EXIT_SUCCESS);
}
/* wait for main loop process to exit. */
pid = waitpid(pid, &childstatus, 0);
shm->parentpid = getpid();
}