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
|
#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"
int check_tainted(void)
{
int fd;
unsigned int ret;
char buffer[11];
buffer[10] = 0; //make sure that we can fit the whole int.
fd = open("/proc/sys/kernel/tainted", O_RDONLY);
if (fd < 0)
return -1;
ret = read(fd, buffer, 10);
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);
}
static void fork_children(void)
{
int pidslot;
static char childname[17];
/* Generate children*/
while (shm->running_childs < shm->max_children) {
int pid = 0;
int fd;
if (shm->spawn_no_more == TRUE)
return;
/* a new child means a new seed, or the new child
* will do the same syscalls as the one in the pidslot it's replacing.
* (special case startup, or we reseed unnecessarily)
*/
if (shm->ready == TRUE)
reseed();
/* Find a space for it in the pid map */
pidslot = find_pid_slot(EMPTY_PIDSLOT);
if (pidslot == PIDSLOT_NOT_FOUND) {
outputerr("## Pid map was full!\n");
dump_pid_slots();
exit(EXIT_FAILURE);
}
if (logging == TRUE) {
fd = fileno(shm->logfiles[pidslot]);
if (ftruncate(fd, 0) == 0)
lseek(fd, 0, SEEK_SET);
}
(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-c%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->mainpid);
if (ret != 0) {
shm->exit_reason = EXIT_SHM_CORRUPTION;
outputerr(BUGTXT "parent (%d) went away!\n", shm->mainpid);
sleep(20000);
}
}
/* Wait for all the children to start up. */
while (shm->ready == FALSE)
sleep(1);
init_child(pidslot);
ret = child_process(pidslot);
output(1, "child exiting.\n");
_exit(ret);
}
shm->running_childs++;
debugf("Created child %d in pidslot %d [total:%d/%d]\n",
shm->pids[pidslot], pidslot,
shm->running_childs, shm->max_children);
if (shm->exit_reason != STILL_RUNNING)
return;
}
shm->ready = TRUE;
debugf("created enough children\n");
}
void reap_child(pid_t childpid)
{
int i;
while (shm->reaper_lock == LOCKED);
shm->reaper_lock = LOCKED;
if (childpid == shm->last_reaped) {
debugf("already reaped %d!\n", childpid);
goto out;
}
i = find_pid_slot(childpid);
if (i == PIDSLOT_NOT_FOUND)
goto out;
debugf("Removing pid %d from pidmap.\n", 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("Nothing changed. children:%d\n", shm->running_childs);
break;
case -1:
if (shm->exit_reason != STILL_RUNNING)
return;
if (errno == ECHILD) {
debugf("All children exited!\n");
for_each_pidslot(i) {
if (shm->pids[i] != EMPTY_PIDSLOT) {
if (pid_alive(shm->pids[i]) == -1) {
debugf("Removing %d from pidmap\n", shm->pids[i]);
shm->pids[i] = EMPTY_PIDSLOT;
shm->running_childs--;
} else {
debugf("%d looks still alive! ignoring.\n", shm->pids[i]);
}
}
}
break;
}
output(0, "error! (%s)\n", strerror(errno));
break;
default:
debugf("Something happened to pid %d\n", childpid);
if (WIFEXITED(childstatus)) {
slot = find_pid_slot(childpid);
if (slot == PIDSLOT_NOT_FOUND) {
/* If we reaped it, it wouldn't show up, so check that. */
if (shm->last_reaped != childpid) {
outputerr("## Couldn't find pid slot for %d\n", childpid);
shm->exit_reason = EXIT_LOST_PID_SLOT;
dump_pid_slots();
}
} else {
debugf("Child %d exited after %ld syscalls.\n", childpid, shm->child_syscall_count[slot]);
reap_child(childpid);
}
break;
} else if (WIFSIGNALED(childstatus)) {
switch (WTERMSIG(childstatus)) {
case SIGALRM:
debugf("got a alarm signal from pid %d\n", childpid);
break;
case SIGFPE:
case SIGSEGV:
case SIGKILL:
case SIGPIPE:
case SIGABRT:
debugf("got a signal from pid %d (%s)\n", childpid, strsignal(WTERMSIG(childstatus)));
reap_child(childpid);
break;
default:
debugf("** Child got an unhandled signal (%d)\n", WTERMSIG(childstatus));
break;
}
break;
} else if (WIFSTOPPED(childstatus)) {
switch (WSTOPSIG(childstatus)) {
case SIGALRM:
debugf("got an alarm signal from pid %d\n", childpid);
break;
case SIGSTOP:
debugf("Sending PTRACE_DETACH (and then KILL)\n");
ptrace(PTRACE_DETACH, childpid, NULL, NULL);
kill(childpid, SIGKILL);
reap_child(childpid);
break;
case SIGFPE:
case SIGSEGV:
case SIGKILL:
case SIGPIPE:
case SIGABRT:
debugf("Child %d was stopped by %s\n", childpid, strsignal(WTERMSIG(childstatus)));
reap_child(childpid);
break;
default:
debugf("Child %d was stopped by unhandled signal (%s).\n", 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.",
"Main process disappeared.",
"UID changed.",
};
static const char * decode_exit(unsigned int reason)
{
return reasons[reason];
}
static void main_loop(void)
{
while (shm->exit_reason == STILL_RUNNING) {
if (shm->spawn_no_more == FALSE) {
if (shm->running_childs < shm->max_children)
fork_children();
/* Periodic regenation of fd's etc. */
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->mainpid = getpid();
output(0, "Main thread is alive.\n");
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();
outputerr("Bailing main loop. Exit reason: %s\n", decode_exit(shm->exit_reason));
_exit(EXIT_SUCCESS);
}
/* wait for main loop process to exit. */
(void)waitpid(pid, &childstatus, 0);
shm->mainpid = 0;
}
|