blob: ae3ce27c82829fb4203dbf585d7e4ca9ddbbfec6 [file] [log] [blame]
Robert Swiecki0e673882016-03-31 18:50:29 +02001/*
2 *
3 * honggfuzz - routines dealing with subprocesses
4 * -----------------------------------------
5 *
Robert Swiecki46288f72018-02-27 17:28:47 +01006 * Author: Robert Swiecki <swiecki@google.com>
7 * Felix Gröbert <groebert@google.com>
Robert Swiecki0e673882016-03-31 18:50:29 +02008 *
Robert Swiecki46288f72018-02-27 17:28:47 +01009 * Copyright 2010-2018 by Google Inc. All Rights Reserved.
Robert Swiecki0e673882016-03-31 18:50:29 +020010 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License. You may obtain
13 * a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
20 * implied. See the License for the specific language governing
21 * permissions and limitations under the License.
22 *
23 */
24
Robert Swiecki0e673882016-03-31 18:50:29 +020025#include "subproc.h"
26
Robert Swiecki1ddf6742017-05-08 21:17:32 +020027#include <errno.h>
Jagger93253f72016-09-01 22:40:12 +020028#include <fcntl.h>
Jaggerc7743512016-08-31 22:00:19 +020029#include <inttypes.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020030#include <signal.h>
31#include <stdio.h>
Jaggerc7743512016-08-31 22:00:19 +020032#include <stdlib.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020033#include <string.h>
Jaggerc7743512016-08-31 22:00:19 +020034#include <sys/resource.h>
Jagger93253f72016-09-01 22:40:12 +020035#include <sys/socket.h>
Jaggerc7743512016-08-31 22:00:19 +020036#include <sys/time.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020037#include <sys/types.h>
38#include <sys/wait.h>
Jaggerc7743512016-08-31 22:00:19 +020039#include <unistd.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020040
Robert Swieckid0fa62c2017-09-28 18:11:05 +020041#include "arch.h"
Robert Swiecki35978ac2017-11-16 18:00:53 +010042#include "fuzz.h"
Robert Swiecki246af3e2018-01-05 14:56:32 +010043#include "libhfcommon/common.h"
44#include "libhfcommon/files.h"
45#include "libhfcommon/log.h"
46#include "libhfcommon/util.h"
Jaggerc7743512016-08-31 22:00:19 +020047
Robert Swiecki4e595fb2017-10-11 17:26:51 +020048extern char** environ;
Robert Swiecki0e673882016-03-31 18:50:29 +020049
Robert Swieckid50ed422017-11-13 23:32:26 +010050const char* subproc_StatusToStr(int status, char* str, size_t len) {
Robert Swiecki0e673882016-03-31 18:50:29 +020051 if (WIFEXITED(status)) {
52 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
53 return str;
54 }
55
56 if (WIFSIGNALED(status)) {
Robert Swiecki0b566112017-10-17 17:39:07 +020057 snprintf(
58 str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status), strsignal(WTERMSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +020059 return str;
60 }
Robert Swiecki0e673882016-03-31 18:50:29 +020061 if (WIFCONTINUED(status)) {
62 snprintf(str, len, "CONTINUED");
63 return str;
64 }
65
66 if (!WIFSTOPPED(status)) {
67 snprintf(str, len, "UNKNOWN STATUS: %d", status);
68 return str;
69 }
70
71 /* Must be in a stopped state */
72 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
73 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020074 strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +020075 return str;
76 }
Robert Swiecki2531d752016-04-19 14:00:17 +020077#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020078#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
79 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
80 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swieckid50ed422017-11-13 23:32:26 +010081 case PTRACE_EVENT_FORK:
82 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
83 strsignal(WSTOPSIG(status)));
84 return str;
85 case PTRACE_EVENT_VFORK:
86 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
87 strsignal(WSTOPSIG(status)));
88 return str;
89 case PTRACE_EVENT_CLONE:
90 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
91 strsignal(WSTOPSIG(status)));
92 return str;
93 case PTRACE_EVENT_EXEC:
94 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
95 strsignal(WSTOPSIG(status)));
96 return str;
97 case PTRACE_EVENT_VFORK_DONE:
98 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
99 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
100 return str;
101 case PTRACE_EVENT_EXIT:
102 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
103 strsignal(WSTOPSIG(status)));
104 return str;
105 case PTRACE_EVENT_SECCOMP:
106 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)",
107 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
108 return str;
109 case PTRACE_EVENT_STOP:
110 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
111 strsignal(WSTOPSIG(status)));
112 return str;
113 default:
114 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
115 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
116 return str;
Robert Swiecki0e673882016-03-31 18:50:29 +0200117 }
118 }
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200119#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200120
Robert Swiecki0b566112017-10-17 17:39:07 +0200121 snprintf(
122 str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +0200123 return str;
124}
Jaggerc7743512016-08-31 22:00:19 +0200125
Robert Swieckid50ed422017-11-13 23:32:26 +0100126bool subproc_persistentModeRoundDone(run_t* run) {
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100127 if (!run->global->exe.persistent) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100128 return false;
129 }
Robert Swiecki599dee12018-01-10 02:21:58 +0100130 uint8_t rcv;
131 if (recv(run->persistentSock, &rcv, sizeof(rcv), MSG_DONTWAIT) != sizeof(rcv)) {
132 return false;
133 }
134 if (rcv == HFdoneTag) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100135 return true;
136 }
Robert Swiecki599dee12018-01-10 02:21:58 +0100137 LOG_F("Received invalid message from the persistent process: '%c' (0x%" PRIx8
138 ") , expected '%c' (0x%" PRIx8 ")",
139 rcv, rcv, HFdoneTag, HFdoneTag);
140 return false;
141}
142
Robert Swiecki7c81c272018-01-11 14:41:04 +0100143static bool subproc_persistentSendFileIndicator(run_t* run) {
Robert Swiecki599dee12018-01-10 02:21:58 +0100144 uint64_t len = (uint64_t)run->dynamicFileSz;
Robert Swieckie7294ca2017-11-11 02:46:32 +0100145 if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) {
Robert Swiecki2036e202017-06-06 00:01:44 +0200146 PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len));
Robert Swiecki940a0802016-11-02 12:42:09 +0100147 return false;
148 }
Robert Swiecki940a0802016-11-02 12:42:09 +0100149 return true;
150}
151
Robert Swiecki599dee12018-01-10 02:21:58 +0100152static bool subproc_PrepareExecv(run_t* run) {
Jaggerc7743512016-08-31 22:00:19 +0200153 /*
Jaggerc7743512016-08-31 22:00:19 +0200154 * The address space limit. If big enough - roughly the size of RAM used
155 */
Robert Swiecki0e6ca612018-01-16 21:48:30 +0100156#ifdef RLIMIT_AS
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100157 if (run->global->exe.asLimit) {
Robert Swieckidc8047c2018-01-07 21:18:26 +0100158 const struct rlimit rl = {
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100159 .rlim_cur = run->global->exe.asLimit * 1024ULL * 1024ULL,
160 .rlim_max = run->global->exe.asLimit * 1024ULL * 1024ULL,
Jaggerc7743512016-08-31 22:00:19 +0200161 };
162 if (setrlimit(RLIMIT_AS, &rl) == -1) {
Robert Swiecki8954afd2017-11-14 18:14:22 +0100163 PLOG_W("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
164 }
165 }
Robert Swiecki0e6ca612018-01-16 21:48:30 +0100166#endif /* ifdef RLIMIT_AS */
167#ifdef RLIMIT_RSS
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100168 if (run->global->exe.rssLimit) {
Robert Swieckidc8047c2018-01-07 21:18:26 +0100169 const struct rlimit rl = {
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100170 .rlim_cur = run->global->exe.rssLimit * 1024ULL * 1024ULL,
171 .rlim_max = run->global->exe.rssLimit * 1024ULL * 1024ULL,
Robert Swiecki8954afd2017-11-14 18:14:22 +0100172 };
173 if (setrlimit(RLIMIT_RSS, &rl) == -1) {
174 PLOG_W("Couldn't enforce the RLIMIT_RSS resource limit, ignoring");
175 }
176 }
Robert Swiecki0e6ca612018-01-16 21:48:30 +0100177#endif /* ifdef RLIMIT_RSS */
178#ifdef RLIMIT_DATA
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100179 if (run->global->exe.dataLimit) {
Robert Swieckidc8047c2018-01-07 21:18:26 +0100180 const struct rlimit rl = {
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100181 .rlim_cur = run->global->exe.dataLimit * 1024ULL * 1024ULL,
182 .rlim_max = run->global->exe.dataLimit * 1024ULL * 1024ULL,
Robert Swiecki8954afd2017-11-14 18:14:22 +0100183 };
Robert Swiecki1f9f2132017-11-27 16:49:37 +0100184 if (setrlimit(RLIMIT_DATA, &rl) == -1) {
Robert Swiecki8954afd2017-11-14 18:14:22 +0100185 PLOG_W("Couldn't enforce the RLIMIT_DATA resource limit, ignoring");
Jaggerc7743512016-08-31 22:00:19 +0200186 }
187 }
Robert Swiecki0e6ca612018-01-16 21:48:30 +0100188#endif /* ifdef RLIMIT_DATA */
Robert Swieckic58ca512018-11-06 17:50:37 +0100189#ifdef RLIMIT_CORE
190 if (run->global->exe.dataLimit) {
191 const struct rlimit rl = {
192 .rlim_cur = run->global->exe.coreLimit * 1024ULL * 1024ULL,
193 .rlim_max = run->global->exe.coreLimit * 1024ULL * 1024ULL,
194 };
195 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
196 PLOG_W("Couldn't enforce the RLIMIT_CORE resource limit, ignoring");
197 }
198 }
199#endif /* ifdef RLIMIT_CORE */
Jaggerc7743512016-08-31 22:00:19 +0200200
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100201 if (run->global->exe.clearEnv) {
Jaggerc7743512016-08-31 22:00:19 +0200202 environ = NULL;
203 }
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100204 for (size_t i = 0; i < ARRAYSIZE(run->global->exe.envs) && run->global->exe.envs[i]; i++) {
205 putenv(run->global->exe.envs[i]);
Jaggerc7743512016-08-31 22:00:19 +0200206 }
207 char fuzzNo[128];
Robert Swieckie7294ca2017-11-11 02:46:32 +0100208 snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, run->fuzzNo);
Jaggerc7743512016-08-31 22:00:19 +0200209 setenv(_HF_THREAD_NO_ENV, fuzzNo, 1);
210
211 setsid();
212
Robert Swiecki6ab8f342018-01-15 23:03:16 +0100213 util_closeStdio(/* close_stdin= */ run->global->exe.nullifyStdio,
Robert Swieckib4333062018-01-15 23:00:45 +0100214 /* close_stdout= */ run->global->exe.nullifyStdio,
215 /* close_stderr= */ run->global->exe.nullifyStdio);
216
Robert Swiecki6526a7d2018-01-15 17:38:58 +0100217 /* The bitmap structure */
Robert Swieckia5b918a2018-03-07 23:59:53 +0100218 if (run->global->feedback.bbFd != -1 && dup2(run->global->feedback.bbFd, _HF_BITMAP_FD) == -1) {
219 PLOG_E("dup2(%d, _HF_BITMAP_FD=%d)", run->global->feedback.bbFd, _HF_BITMAP_FD);
Robert Swiecki6526a7d2018-01-15 17:38:58 +0100220 return false;
Jaggerc7743512016-08-31 22:00:19 +0200221 }
222
Robert Swiecki6526a7d2018-01-15 17:38:58 +0100223 /* The input file to _HF_INPUT_FD */
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100224 if (run->global->exe.persistent && dup2(run->dynamicFileFd, _HF_INPUT_FD) == -1) {
Robert Swiecki0a01ea72018-01-11 01:50:18 +0100225 PLOG_E("dup2('%d', _HF_INPUT_FD='%d')", run->dynamicFileFd, _HF_INPUT_FD);
226 return false;
227 }
Robert Swiecki599dee12018-01-10 02:21:58 +0100228
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100229 sigset_t sset;
230 sigemptyset(&sset);
231 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
232 PLOG_W("sigprocmask(empty_set)");
233 }
234
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100235 if (!run->global->exe.persistent) {
Robert Swiecki1f1a2f92018-01-15 15:26:37 +0100236 if ((run->dynamicFileCopyFd = files_writeBufToTmpFile(
237 run->global->io.workDir, run->dynamicFile, run->dynamicFileSz, 0)) == -1) {
238 LOG_E("Couldn't save data to a temporary file");
239 return false;
240 }
241 if (run->global->exe.fuzzStdin && dup2(run->dynamicFileCopyFd, STDIN_FILENO) == -1) {
242 PLOG_E("dup2(_HF_INPUT_FD=%d, STDIN_FILENO=%d)", run->dynamicFileCopyFd, STDIN_FILENO);
243 return false;
244 }
Robert Swiecki3975afe2018-01-10 04:32:21 +0100245 }
Robert Swieckic8a95362018-01-10 02:29:49 +0100246
Jaggerc7743512016-08-31 22:00:19 +0200247 return true;
248}
Jagger93253f72016-09-01 22:40:12 +0200249
Robert Swieckid50ed422017-11-13 23:32:26 +0100250static bool subproc_New(run_t* run) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100251 run->pid = run->persistentPid;
dobinedf9f8d2018-01-21 13:57:02 +0100252 if (run->pid != 0 && run->hasCrashed == false) {
Jagger93253f72016-09-01 22:40:12 +0200253 return true;
254 }
dobinedf9f8d2018-01-21 13:57:02 +0100255
256 LOG_D("SocketFuzzer: subproc_new: Start New Process");
257 run->hasCrashed = false;
Robert Swieckie7294ca2017-11-11 02:46:32 +0100258 run->tmOutSignaled = false;
Jagger93253f72016-09-01 22:40:12 +0200259
260 int sv[2];
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100261 if (run->global->exe.persistent) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100262 if (run->persistentSock != -1) {
263 close(run->persistentSock);
Jagger82a20c02016-09-02 01:36:39 +0200264 }
265
Jaggere4dad652016-09-10 03:35:50 +0200266 int sock_type = SOCK_STREAM;
267#if defined(SOCK_CLOEXEC)
268 sock_type |= SOCK_CLOEXEC;
269#endif
270 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200271 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
272 return false;
273 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100274 run->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200275 }
276
Robert Swiecki158fe042018-01-10 02:33:56 +0100277 LOG_D("Forking new process for thread: %" PRId32, run->fuzzNo);
278
Robert Swiecki78633d12017-11-13 23:24:55 +0100279 run->pid = arch_fork(run);
Robert Swieckie7294ca2017-11-11 02:46:32 +0100280 if (run->pid == -1) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200281 PLOG_E("Couldn't fork");
282 return false;
Jagger93253f72016-09-01 22:40:12 +0200283 }
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200284 /* The child process */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100285 if (!run->pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200286 logMutexReset();
287 /*
288 * Reset sighandlers, and set alarm(1). It's a guarantee against dead-locks
289 * in the child, where we ensure here that the child process will either
290 * execve or get signaled by SIGALRM within 1 second.
Robert Swieckic337d4f2017-06-01 13:09:41 +0200291 *
292 * Those deadlocks typically stem from the fact, that malloc() can behave weirdly
293 * when fork()-ing a single thread of a process: e.g. with glibc < 2.24
294 * (or, Ubuntu's 2.23-0ubuntu6). For more see
295 * http://changelogs.ubuntu.com/changelogs/pool/main/g/glibc/glibc_2.23-0ubuntu7/changelog
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200296 */
297 alarm(1);
298 signal(SIGALRM, SIG_DFL);
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200299
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100300 if (run->global->exe.persistent) {
Jagger93253f72016-09-01 22:40:12 +0200301 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
302 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
303 }
304 close(sv[0]);
305 close(sv[1]);
306 }
307
Robert Swiecki599dee12018-01-10 02:21:58 +0100308 if (!subproc_PrepareExecv(run)) {
Jagger93253f72016-09-01 22:40:12 +0200309 LOG_E("subproc_PrepareExecv() failed");
310 exit(EXIT_FAILURE);
311 }
Robert Swiecki78633d12017-11-13 23:24:55 +0100312 if (!arch_launchChild(run)) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200313 LOG_E("Error launching child process");
Robert Swiecki82c707c2017-11-14 16:36:23 +0100314 kill(run->global->threads.mainPid, SIGTERM);
Robert Swieckia375f4b2017-06-01 13:20:54 +0200315 _exit(1);
Jagger93253f72016-09-01 22:40:12 +0200316 }
Robert Swiecki42cc9822017-02-12 22:20:46 +0100317 abort();
Jagger93253f72016-09-01 22:40:12 +0200318 }
Robert Swieckic337d4f2017-06-01 13:09:41 +0200319
320 /* Parent */
Robert Swiecki158fe042018-01-10 02:33:56 +0100321 LOG_D("Launched new process, PID: %d, thread: %" PRId32 " (concurrency: %zd)", run->pid,
322 run->fuzzNo, run->global->threads.threadsMax);
Robert Swiecki5eeb29b2018-01-21 16:07:06 +0100323
Robert Swiecki5e26bd92018-03-02 12:09:34 +0100324 if (run->global->socketFuzzer.enabled) {
Robert Swiecki5eeb29b2018-01-21 16:07:06 +0100325 /* (dobin): Don't know why, but this is important */
dobinedf9f8d2018-01-21 13:57:02 +0100326 run->persistentPid = run->pid;
327 }
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100328 if (run->global->exe.persistent) {
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300329 close(sv[1]);
Robert Swieckie7294ca2017-11-11 02:46:32 +0100330 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)run->pid);
331 run->persistentPid = run->pid;
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300332 }
Jagger93253f72016-09-01 22:40:12 +0200333
Robert Swiecki78633d12017-11-13 23:24:55 +0100334 arch_prepareParentAfterFork(run);
Robert Swiecki810d2c92017-06-01 21:37:27 +0200335
Jagger93253f72016-09-01 22:40:12 +0200336 return true;
337}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200338
Robert Swieckid50ed422017-11-13 23:32:26 +0100339bool subproc_Run(run_t* run) {
Robert Swieckif2da05a2018-01-12 03:01:09 +0100340 run->timeStartedMillis = util_timeNowMillis();
341
Robert Swiecki78633d12017-11-13 23:24:55 +0100342 if (!subproc_New(run)) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100343 LOG_E("subproc_New()");
Jaggerc4729592016-09-02 01:52:30 +0200344 return false;
345 }
Jaggerc4729592016-09-02 01:52:30 +0200346
Robert Swiecki78633d12017-11-13 23:24:55 +0100347 arch_prepareParent(run);
Robert Swiecki599dee12018-01-10 02:21:58 +0100348
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100349 if (run->global->exe.persistent && !subproc_persistentSendFileIndicator(run)) {
Robert Swiecki599dee12018-01-10 02:21:58 +0100350 LOG_W("Could not send file size to the persistent process");
Robert Swieckie7294ca2017-11-11 02:46:32 +0100351 kill(run->persistentPid, SIGKILL);
Jaggerc4729592016-09-02 01:52:30 +0200352 }
Robert Swiecki78633d12017-11-13 23:24:55 +0100353 arch_reapChild(run);
Robert Swiecki940a0802016-11-02 12:42:09 +0100354
355 return true;
Jaggerd1fc9ed2016-09-02 01:06:51 +0200356}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200357
Robert Swieckid50ed422017-11-13 23:32:26 +0100358uint8_t subproc_System(run_t* run, const char* const argv[]) {
Robert Swiecki78633d12017-11-13 23:24:55 +0100359 pid_t pid = arch_fork(run);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200360 if (pid == -1) {
361 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200362 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200363 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200364 if (!pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200365 logMutexReset();
Robert Swiecki5dbab882017-10-20 16:00:20 +0200366
Robert Swiecki3d66b7e2018-01-17 00:21:59 +0100367 setsid();
368 util_closeStdio(
369 /* close_stdin= */ true, /* close_stdout= */ false, /* close_stderr= */ false);
370
Robert Swiecki5dbab882017-10-20 16:00:20 +0200371 sigset_t sset;
372 sigemptyset(&sset);
373 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
374 PLOG_W("sigprocmask(empty_set)");
375 }
376
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200377 execv(argv[0], (char* const*)&argv[0]);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200378 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200379 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200380 }
381
382 int status;
383 int flags = 0;
384#if defined(__WNOTHREAD)
385 flags |= __WNOTHREAD;
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200386#endif /* defined(__WNOTHREAD) */
Robert Swiecki54333cd2016-09-07 17:51:48 +0200387
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200388 for (;;) {
389 int ret = wait4(pid, &status, flags, NULL);
390 if (ret == -1 && errno == EINTR) {
391 continue;
392 }
393 if (ret == -1) {
394 PLOG_E("wait4() for process PID: %d", (int)pid);
395 return 255;
396 }
397 if (ret != pid) {
398 LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid);
399 return 255;
400 }
401 if (WIFSIGNALED(status)) {
402 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
403 return (100 + WTERMSIG(status));
404 }
405 if (WIFEXITED(status)) {
406 if (WEXITSTATUS(status) == 0) {
407 return 0U;
408 }
409 LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status));
410 return 1U;
411 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200412
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200413 LOG_D("wait4() returned with status: %d", status);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200414 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200415}
Robert Swieckifddc0862016-10-27 12:42:46 +0200416
Robert Swieckid50ed422017-11-13 23:32:26 +0100417void subproc_checkTimeLimit(run_t* run) {
Robert Swiecki371e1292017-12-18 01:10:33 +0100418 if (run->global->timing.tmOut == 0) {
Robert Swieckifddc0862016-10-27 12:42:46 +0200419 return;
420 }
421
422 int64_t curMillis = util_timeNowMillis();
Robert Swieckie7294ca2017-11-11 02:46:32 +0100423 int64_t diffMillis = curMillis - run->timeStartedMillis;
Robert Swiecki013bc9c2016-12-12 17:31:06 +0100424
Robert Swiecki371e1292017-12-18 01:10:33 +0100425 if (run->tmOutSignaled && (diffMillis > ((run->global->timing.tmOut + 1) * 1000))) {
Robert Swiecki66d3e252017-07-06 01:53:40 +0200426 /* Has this instance been already signaled due to timeout? Just, SIGKILL it */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100427 LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", run->pid);
428 kill(run->pid, SIGKILL);
Robert Swiecki66d3e252017-07-06 01:53:40 +0200429 return;
430 }
431
Robert Swiecki371e1292017-12-18 01:10:33 +0100432 if ((diffMillis > (run->global->timing.tmOut * 1000)) && !run->tmOutSignaled) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100433 run->tmOutSignaled = true;
Robert Swiecki78633d12017-11-13 23:24:55 +0100434 LOG_W("PID %d took too much time (limit %ld s). Killing it with %s", run->pid,
Robert Swieckib8db4b42018-01-16 22:13:30 +0100435 (long)run->global->timing.tmOut,
436 run->global->timing.tmoutVTALRM ? "SIGVTALRM" : "SIGKILL");
Robert Swieckieba27172017-12-18 01:12:02 +0100437 if (run->global->timing.tmoutVTALRM) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100438 kill(run->pid, SIGVTALRM);
Robert Swieckie84b6452016-12-12 12:42:04 +0100439 } else {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100440 kill(run->pid, SIGKILL);
Robert Swieckie84b6452016-12-12 12:42:04 +0100441 }
Robert Swiecki2542dc02017-11-14 03:35:59 +0100442 ATOMIC_POST_INC(run->global->cnts.timeoutedCnt);
Robert Swieckifddc0862016-10-27 12:42:46 +0200443 }
444}
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100445
Robert Swieckid50ed422017-11-13 23:32:26 +0100446void subproc_checkTermination(run_t* run) {
Robert Swiecki0dde76d2017-11-16 19:25:44 +0100447 if (fuzz_isTerminating()) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100448 LOG_D("Killing PID: %d", (int)run->pid);
449 kill(run->pid, SIGKILL);
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100450 }
451}