-
Notifications
You must be signed in to change notification settings - Fork 241
/
child.c
457 lines (369 loc) · 9.76 KB
/
child.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
/*
* Each process that gets forked runs this code.
*/
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/prctl.h>
#include "arch.h"
#include "child.h"
#include "list.h"
#include "log.h"
#include "maps.h"
#include "params.h"
#include "pids.h"
#include "random.h"
#include "shm.h"
#include "signals.h"
#include "syscall.h"
#include "tables.h"
#include "trinity.h" // ARRAY_SIZE
#include "uid.h"
#include "utils.h" // zmalloc
/*
* Provide temporary immunity from the reaper
* This is useful if we're going to do something that might take
* longer than the time the reaper is prepared to wait, especially if
* we're doing something critical, like handling a lock, or dumping a log.
*/
void set_dontkillme(struct childdata *child, bool state)
{
if (child == NULL) /* possible, we might be the mainpid */
return;
child->dontkillme = state;
/* bump the progress indicator */
clock_gettime(CLOCK_MONOTONIC, &child->tp);
}
/*
* For the child processes, we don't want core dumps (unless we're running with -D)
* This is because it's not uncommon for us to get segfaults etc when we're doing
* syscalls with garbage for arguments.
*/
static void disable_coredumps(void)
{
struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
if (shm->debug == TRUE) {
(void)signal(SIGABRT, SIG_DFL);
(void)signal(SIGSEGV, SIG_DFL);
return;
}
if (setrlimit(RLIMIT_CORE, &limit) != 0)
perror( "setrlimit(RLIMIT_CORE)" );
prctl(PR_SET_DUMPABLE, FALSE);
}
/*
* We reenable core dumps when we're about to exit a child.
* TODO: Maybe narrow the disable/enable pair to just around do_syscall ?
*/
static void enable_coredumps(void)
{
struct rlimit limit = {
.rlim_cur = RLIM_INFINITY,
.rlim_max = RLIM_INFINITY
};
if (shm->debug == TRUE)
return;
prctl(PR_SET_DUMPABLE, TRUE);
(void) setrlimit(RLIMIT_CORE, &limit);
}
/*
* Enable the kernels fault-injection code for our child process.
* (Assumes you've set everything else up by hand).
*/
static void set_make_it_fail(void)
{
int fd;
const char *buf = "1";
/* If we failed last time, it's probably because we don't
* have fault-injection enabled, so don't bother trying in future.
*/
if (shm->dont_make_it_fail == TRUE)
return;
fd = open("/proc/self/make-it-fail", O_WRONLY);
if (fd == -1) {
shm->dont_make_it_fail = TRUE;
return;
}
if (write(fd, buf, 1) == -1) {
if (errno != EPERM)
outputerr("writing to /proc/self/make-it-fail failed! (%s)\n", strerror(errno));
shm->dont_make_it_fail = TRUE;
}
close(fd);
}
/*
* We call this occasionally to set some FPU state, in the hopes that we
* might tickle some weird FPU/scheduler related bugs
*/
static void use_fpu(void)
{
double x = 0;
asm volatile("":"+m" (x));
x += 1;
asm volatile("":"+m" (x));
}
/*
* Tweak the oom_score_adj setting for our child so that there's a higher
* chance that the oom-killer kills our processes rather than something
* more important.
*/
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);
}
/*
* Wipe out any state left from a previous child running in this slot.
*/
void clean_childdata(struct childdata *child)
{
memset(&child->syscall, 0, sizeof(struct syscallrecord));
child->logdirty = FALSE;
child->seed = 0;
child->kill_count = 0;
child->dontkillme = FALSE;
child->xcpu_count = 0;
child->op_nr = 0;
child->dropped_privs = FALSE;
clock_gettime(CLOCK_MONOTONIC, &child->tp);
}
static void bind_child_to_cpu(struct childdata *child)
{
cpu_set_t set;
unsigned int cpudest;
pid_t pid = pids[child->num];
if (no_bind_to_cpu == TRUE)
return;
if (sched_getaffinity(pid, sizeof(set), &set) != 0)
return;
if (child->num > num_online_cpus)
cpudest = child->num % num_online_cpus;
else
cpudest = child->num;
CPU_ZERO(&set);
CPU_SET(cpudest, &set);
sched_setaffinity(pid, sizeof(set), &set);
}
/*
* Called from the fork_children loop in the main process.
*/
static void init_child(struct childdata *child, int childno)
{
pid_t pid = getpid();
char childname[17];
unsigned int i;
for_each_child(i) {
if (child->num != i)
mprotect(shm->children[i], sizeof(struct childdata), PROT_READ);
}
mprotect(pids, max_children * sizeof(int), PROT_READ);
/* Wait for parent to set our childno */
while (pids[childno] != pid) {
/* Make sure parent is actually alive to wait for us. */
if (pid_alive(mainpid) == FALSE) {
panic(EXIT_SHM_CORRUPTION);
outputerr("BUG!: parent (%d) went away!\n", mainpid);
sleep(20000);
}
}
set_seed(child);
init_object_lists(OBJ_LOCAL);
init_child_mappings();
dirty_random_mapping();
if (RAND_BOOL())
bind_child_to_cpu(child);
memset(childname, 0, sizeof(childname));
sprintf(childname, "trinity-c%d", childno);
prctl(PR_SET_NAME, (unsigned long) &childname);
oom_score_adj(500);
/* Wait for all the children to start up. */
while (shm->ready == FALSE)
sleep(1);
set_make_it_fail();
if (RAND_BOOL())
use_fpu();
mask_signals_child();
disable_coredumps();
if (RAND_BOOL()) {
unshare(CLONE_NEWNS);
unshare(CLONE_NEWIPC);
unshare(CLONE_IO);
unshare(CLONE_NEWNET);
}
/*
if (shm->unshare_perm_err == FALSE) {
if (RAND_BOOL()) {
int ret = unshare(CLONE_NEWUSER);
if (ret != 0)
output(0, "couldn't unshare: %s\n", strerror(errno));
if (ret == -EPERM)
shm->unshare_perm_err = TRUE;
}
}
*/
if (orig_uid == 0)
child->dropped_privs = FALSE;
}
/*
* Sanity check to make sure that the main process is still around
* to wait for us.
*/
static void check_parent_pid(void)
{
pid_t pid, ppid;
ppid = getppid();
if (ppid == mainpid)
return;
pid = getpid();
/* TODO: it'd be neat to do stuff inside pidns's, but right now
* we shit ourselves when we exit and get reparented to pid 1
*/
if (pid == ppid) {
debugf("pid became ppid! exiting child.\n");
_exit(EXIT_FAILURE);
}
if (ppid < 2) {
debugf("ppid == %d. pidns? exiting child.\n", ppid);
_exit(EXIT_FAILURE);;
}
lock(&shm->buglock);
if (shm->exit_reason == EXIT_REPARENT_PROBLEM)
goto out;
output(0, "BUG!: CHILD (pid:%d) GOT REPARENTED! "
"main pid:%d. ppid=%d\n",
pid, mainpid, ppid);
if (pid_alive(mainpid) == FALSE)
output(0, "main pid %d is dead.\n", mainpid);
panic(EXIT_REPARENT_PROBLEM);
out:
unlock(&shm->buglock);
_exit(EXIT_FAILURE);
}
/*
* Here we call various functions that perform checks/changes that
* we don't want to happen on every iteration of the child loop.
*/
static void periodic_work(void)
{
static unsigned int periodic_counter = 0;
periodic_counter++;
if (periodic_counter < 10)
return;
/* Every ten iterations. */
if (!(periodic_counter % 10))
check_parent_pid();
/* Every 100 iterations. */
// if (!(periodic_counter % 100))
// dirty_random_mapping();
if (periodic_counter == 1000)
periodic_counter = 0;
}
/*
* We jump here on return from a signal. We do all the stuff here that we
* otherwise couldn't do in a signal handler.
*
* FIXME: when we have different child ops, we're going to need to redo the progress detector.
*/
static bool handle_sigreturn(int sigwas)
{
struct childdata *child = this_child();
struct syscallrecord *rec;
static unsigned int count = 0;
static unsigned int last = 0;
rec = &child->syscall;
/* If we held a lock before the signal happened, drop it. */
bust_lock(&rec->lock);
/* Check if we're blocked because we were stuck on an fd. */
lock(&rec->lock);
if (check_if_fd(child, rec) == TRUE) {
/* avoid doing it again from other threads. */
shm->fd_lifetime = 0;
/* TODO: Somehow mark the fd in the parent not to be used again too. */
}
unlock(&rec->lock);
/* Check if we're making any progress at all. */
if (child->op_nr == last) {
count++;
//output(1, "no progress for %d tries.\n", count);
} else {
count = 0;
last = child->op_nr;
}
if (count == 10) {
output(1, "no progress for 10 tries, exiting child.\n");
return FALSE;
}
if (child->kill_count > 0) {
output(1, "[%d] Missed a kill signal, exiting\n", getpid());
return FALSE;
}
if (sigwas == SIGHUP)
return FALSE;
if (sigwas != SIGALRM)
output(1, "[%d] Back from signal handler! (sig was %s)\n", getpid(), strsignal(sigwas));
else {
child->op_nr++;
}
return TRUE;
}
/*
* This is the child main loop, entered after init_child has completed
* from the fork_children() loop.
* We also re-enter it from the signal handler code if something happened.
*/
#define NEW_OP_COUNT 100000
void child_process(struct childdata *child, int childno)
{
int ret;
init_child(child, childno);
ret = sigsetjmp(ret_jump, 1);
if (ret != 0) {
if (child->xcpu_count == 100) {
debugf("Child %d [%d] got 100 XCPUs. Exiting child.\n", child->num, pids[child->num]);
goto out;
}
if (handle_sigreturn(ret) == FALSE)
goto out; // Exit the child, things are getting too weird.
}
while (shm->exit_reason == STILL_RUNNING) {
/* If the parent reseeded, we should reflect the latest seed too. */
if (shm->seed != child->seed) {
//output(0, "child %d reseeded to %x\n", child->num, child->seed);
set_seed(child);
}
periodic_work();
/* timestamp, and do the syscall */
clock_gettime(CLOCK_MONOTONIC, &child->tp);
ret = random_syscall(child);
child->op_nr++;
if (ret == FAIL)
goto out;
if (syscalls_todo) {
if (shm->stats.op_count >= syscalls_todo) {
shm->exit_reason = EXIT_REACHED_COUNT;
goto out;
}
}
}
enable_coredumps();
/* If we're exiting because we tainted, wait here for it to be done. */
while (shm->postmortem_in_progress == TRUE) {
/* Make sure the main process is still around. */
if (pid_alive(mainpid) == FALSE)
goto out;
usleep(1);
}
out:
shutdown_child_logging(child);
debugf("child %d %d exiting.\n", childno, getpid());
}