blob: b98efeea9e7fbe7fa889d39151d497e8d9b12f45 [file] [log] [blame]
Robert Swiecki0e673882016-03-31 18:50:29 +02001/*
2 *
3 * honggfuzz - routines dealing with subprocesses
4 * -----------------------------------------
5 *
6 * Author:
7 * Robert Swiecki <swiecki@google.com>
8 * Felix Gröbert <groebert@google.com>
9 *
10 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License. You may obtain
14 * a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
21 * implied. See the License for the specific language governing
22 * permissions and limitations under the License.
23 *
24 */
25
26#include "common.h"
27#include "subproc.h"
28
Robert Swiecki1ddf6742017-05-08 21:17:32 +020029#include <errno.h>
Jagger93253f72016-09-01 22:40:12 +020030#include <fcntl.h>
Jaggerc7743512016-08-31 22:00:19 +020031#include <inttypes.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020032#include <signal.h>
33#include <stdio.h>
Jaggerc7743512016-08-31 22:00:19 +020034#include <stdlib.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020035#include <string.h>
Jaggerc7743512016-08-31 22:00:19 +020036#include <sys/resource.h>
Jagger93253f72016-09-01 22:40:12 +020037#include <sys/socket.h>
Jaggerc7743512016-08-31 22:00:19 +020038#include <sys/time.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020039#include <sys/types.h>
40#include <sys/wait.h>
Jaggerc7743512016-08-31 22:00:19 +020041#include <unistd.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020042
Jagger93253f72016-09-01 22:40:12 +020043#include "arch.h"
Jaggerc4729592016-09-02 01:52:30 +020044#include "files.h"
Robert Swiecki0e673882016-03-31 18:50:29 +020045#include "log.h"
Jaggerc7743512016-08-31 22:00:19 +020046#include "sancov.h"
47#include "util.h"
48
49extern char **environ;
Robert Swiecki0e673882016-03-31 18:50:29 +020050
51const char *subproc_StatusToStr(int status, char *str, size_t len)
52{
53 if (WIFEXITED(status)) {
54 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
55 return str;
56 }
57
58 if (WIFSIGNALED(status)) {
59 snprintf(str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status),
60 strsignal(WTERMSIG(status)));
61 return str;
62 }
Robert Swiecki0e673882016-03-31 18:50:29 +020063 if (WIFCONTINUED(status)) {
64 snprintf(str, len, "CONTINUED");
65 return str;
66 }
67
68 if (!WIFSTOPPED(status)) {
69 snprintf(str, len, "UNKNOWN STATUS: %d", status);
70 return str;
71 }
72
73 /* Must be in a stopped state */
74 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
75 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
76 strsignal(WSTOPSIG(status)));
77 return str;
78 }
Robert Swiecki2531d752016-04-19 14:00:17 +020079#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020080#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
81 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
82 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swiecki2531d752016-04-19 14:00:17 +020083 case PTRACE_EVENT_FORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020084 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
85 strsignal(WSTOPSIG(status)));
86 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020087 case PTRACE_EVENT_VFORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020088 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
89 strsignal(WSTOPSIG(status)));
90 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020091 case PTRACE_EVENT_CLONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020092 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
93 strsignal(WSTOPSIG(status)));
94 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020095 case PTRACE_EVENT_EXEC:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020096 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
97 strsignal(WSTOPSIG(status)));
98 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020099 case PTRACE_EVENT_VFORK_DONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200100 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
101 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
102 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200103 case PTRACE_EVENT_EXIT:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200104 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
105 strsignal(WSTOPSIG(status)));
106 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200107 case PTRACE_EVENT_SECCOMP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200108 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status),
109 strsignal(WSTOPSIG(status)));
110 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200111 case PTRACE_EVENT_STOP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200112 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki0e673882016-03-31 18:50:29 +0200113 strsignal(WSTOPSIG(status)));
114 return str;
115 default:
116 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
117 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
118 return str;
119 }
120 }
Robert Swiecki2531d752016-04-19 14:00:17 +0200121#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200122
123 snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status),
124 strsignal(WSTOPSIG(status)));
125 return str;
126}
Jaggerc7743512016-08-31 22:00:19 +0200127
Robert Swiecki940a0802016-11-02 12:42:09 +0100128bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
129{
130 if (hfuzz->persistent == false) {
131 return false;
132 }
Robert Swieckibcfdb972016-11-23 20:33:41 +0100133 char z;
Robert Swiecki940a0802016-11-02 12:42:09 +0100134 if (recv(fuzzer->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) {
135 LOG_D("Persistent mode round finished");
136 return true;
137 }
138 return false;
139}
140
141static bool subproc_persistentSendFile(fuzzer_t * fuzzer)
142{
143 uint32_t len = (uint64_t) fuzzer->dynamicFileSz;
144 if (files_writeToFd(fuzzer->persistentSock, (uint8_t *) & len, sizeof(len)) == false) {
145 return false;
146 }
147 if (files_writeToFd(fuzzer->persistentSock, fuzzer->dynamicFile, fuzzer->dynamicFileSz) ==
148 false) {
149 return false;
150 }
151 return true;
152}
153
Jaggerc7743512016-08-31 22:00:19 +0200154bool subproc_PrepareExecv(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, const char *fileName)
155{
156 /*
Jaggerc7743512016-08-31 22:00:19 +0200157 * The address space limit. If big enough - roughly the size of RAM used
158 */
159 if (hfuzz->asLimit) {
160 struct rlimit rl = {
161 .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL,
162 .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL,
163 };
164 if (setrlimit(RLIMIT_AS, &rl) == -1) {
165 PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
166 }
167 }
168
169 if (hfuzz->nullifyStdio) {
170 util_nullifyStdio();
171 }
172
173 if (hfuzz->fuzzStdin) {
174 /*
175 * Uglyyyyyy ;)
176 */
177 if (!util_redirectStdin(fileName)) {
178 return false;
179 }
180 }
181
182 if (hfuzz->clearEnv) {
183 environ = NULL;
184 }
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200185 if (sanitizers_prepareExecve(hfuzz) == false) {
186 LOG_E("sanitizers_prepareExecve() failed");
Jaggerc7743512016-08-31 22:00:19 +0200187 return false;
188 }
189 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
190 putenv(hfuzz->envs[i]);
191 }
192 char fuzzNo[128];
193 snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, fuzzer->fuzzNo);
194 setenv(_HF_THREAD_NO_ENV, fuzzNo, 1);
195
196 setsid();
197
198 if (hfuzz->bbFd != -1) {
199 if (dup2(hfuzz->bbFd, _HF_BITMAP_FD) == -1) {
200 PLOG_F("dup2('%d', %d)", hfuzz->bbFd, _HF_BITMAP_FD);
201 }
Jaggerae663be2016-09-10 03:27:10 +0200202 close(hfuzz->bbFd);
Jaggerc7743512016-08-31 22:00:19 +0200203 }
204
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100205 sigset_t sset;
206 sigemptyset(&sset);
207 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
208 PLOG_W("sigprocmask(empty_set)");
209 }
210
Jaggerc7743512016-08-31 22:00:19 +0200211 return true;
212}
Jagger93253f72016-09-01 22:40:12 +0200213
Robert Swiecki940a0802016-11-02 12:42:09 +0100214static bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jagger93253f72016-09-01 22:40:12 +0200215{
216 fuzzer->pid = fuzzer->persistentPid;
217 if (fuzzer->pid != 0) {
218 return true;
219 }
Gergely Nagyf95cda62016-12-12 23:40:16 +0100220 fuzzer->tmOutSignaled = false;
Jagger93253f72016-09-01 22:40:12 +0200221
222 int sv[2];
223 if (hfuzz->persistent) {
Jagger82a20c02016-09-02 01:36:39 +0200224 if (fuzzer->persistentSock != -1) {
225 close(fuzzer->persistentSock);
226 }
227
Jaggere4dad652016-09-10 03:35:50 +0200228 int sock_type = SOCK_STREAM;
229#if defined(SOCK_CLOEXEC)
230 sock_type |= SOCK_CLOEXEC;
231#endif
232 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200233 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
234 return false;
235 }
Robert Swieckic7a95f82016-10-27 19:29:35 +0200236 fuzzer->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200237 }
238
239 fuzzer->pid = arch_fork(hfuzz, fuzzer);
240 if (fuzzer->pid == -1) {
241 PLOG_F("Couldn't fork");
242 }
Robert Swieckia71a4992016-09-02 14:47:55 +0200243 // Child
Jagger93253f72016-09-01 22:40:12 +0200244 if (!fuzzer->pid) {
245 if (hfuzz->persistent) {
246 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
247 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
248 }
249 close(sv[0]);
250 close(sv[1]);
251 }
252
253 if (!subproc_PrepareExecv(hfuzz, fuzzer, fuzzer->fileName)) {
254 LOG_E("subproc_PrepareExecv() failed");
255 exit(EXIT_FAILURE);
256 }
257 if (!arch_launchChild(hfuzz, fuzzer->fileName)) {
258 LOG_E("Error launching child process");
Robert Swieckia7ecf162017-02-12 23:17:41 +0100259 kill(hfuzz->mainPid, SIGTERM);
Robert Swiecki99071002017-02-13 17:16:18 +0100260 exit(EXIT_FAILURE);
Jagger93253f72016-09-01 22:40:12 +0200261 }
Robert Swiecki42cc9822017-02-12 22:20:46 +0100262 abort();
Jagger93253f72016-09-01 22:40:12 +0200263 }
Robert Swieckia71a4992016-09-02 14:47:55 +0200264 // Parent
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300265 LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax);
Jagger68f33592016-09-03 04:52:16 +0200266
Jagger93253f72016-09-01 22:40:12 +0200267 if (hfuzz->persistent) {
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300268 close(sv[1]);
Robert Swiecki2a7da342016-10-27 19:27:30 +0200269 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid);
270 fuzzer->persistentPid = fuzzer->pid;
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300271 }
Jagger93253f72016-09-01 22:40:12 +0200272
273 return true;
274}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200275
Robert Swiecki940a0802016-11-02 12:42:09 +0100276bool subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jaggerc4729592016-09-02 01:52:30 +0200277{
Robert Swiecki940a0802016-11-02 12:42:09 +0100278 if (subproc_New(hfuzz, fuzzer) == false) {
279 LOG_E("subproc_New()");
Jaggerc4729592016-09-02 01:52:30 +0200280 return false;
281 }
Jaggerc4729592016-09-02 01:52:30 +0200282
Jaggerd1fc9ed2016-09-02 01:06:51 +0200283 arch_prepareChild(hfuzz, fuzzer);
Jaggerc4729592016-09-02 01:52:30 +0200284 if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) {
285 LOG_W("Could not send file contents to the persistent process");
286 }
Jaggerd1fc9ed2016-09-02 01:06:51 +0200287 arch_reapChild(hfuzz, fuzzer);
Robert Swiecki940a0802016-11-02 12:42:09 +0100288
289 return true;
Jaggerd1fc9ed2016-09-02 01:06:51 +0200290}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200291
292uint8_t subproc_System(const char *const argv[])
293{
294 pid_t pid = fork();
295 if (pid == -1) {
296 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200297 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200298 }
299
300 if (!pid) {
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100301 sigset_t sset;
302 sigemptyset(&sset);
303 sigprocmask(SIG_SETMASK, &sset, NULL);
Robert Swiecki87ca2112017-04-27 12:03:53 +0200304 setsid();
Robert Swiecki54333cd2016-09-07 17:51:48 +0200305 execv(argv[0], (char *const *)&argv[0]);
306 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200307 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200308 }
309
310 int status;
311 int flags = 0;
312#if defined(__WNOTHREAD)
313 flags |= __WNOTHREAD;
314#endif /* defined(__WNOTHREAD) */
Robert Swiecki54333cd2016-09-07 17:51:48 +0200315
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200316 for (;;) {
317 int ret = wait4(pid, &status, flags, NULL);
318 if (ret == -1 && errno == EINTR) {
319 continue;
320 }
321 if (ret == -1) {
322 PLOG_E("wait4() for process PID: %d", (int)pid);
323 return 255;
324 }
325 if (ret != pid) {
326 LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid);
327 return 255;
328 }
329 if (WIFSIGNALED(status)) {
330 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
331 return (100 + WTERMSIG(status));
332 }
333 if (WIFEXITED(status)) {
334 if (WEXITSTATUS(status) == 0) {
335 return 0U;
336 }
337 LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status));
338 return 1U;
339 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200340
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200341 LOG_D("wait4() returned with status: %d", status);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200342 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200343}
Robert Swieckifddc0862016-10-27 12:42:46 +0200344
345void subproc_checkTimeLimit(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
346{
347 if (hfuzz->tmOut == 0) {
348 return;
349 }
350
351 int64_t curMillis = util_timeNowMillis();
352 int64_t diffMillis = curMillis - fuzzer->timeStartedMillis;
353 if (diffMillis > (hfuzz->tmOut * 1000)) {
Robert Swiecki013bc9c2016-12-12 17:31:06 +0100354 /* Has this instance been already signaled due to timeout? Just, SIGKILL it */
355 if (fuzzer->tmOutSignaled) {
356 LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL",
357 fuzzer->pid);
358 kill(fuzzer->pid, SIGKILL);
359 return;
360 }
361 fuzzer->tmOutSignaled = true;
362
Robert Swieckie84b6452016-12-12 12:42:04 +0100363 LOG_W("PID %d took too much time (limit %ld s). Killing it", fuzzer->pid, hfuzz->tmOut);
364 if (hfuzz->tmout_vtalrm) {
365 kill(fuzzer->pid, SIGVTALRM);
366 } else {
367 kill(fuzzer->pid, SIGKILL);
368 }
Robert Swieckifddc0862016-10-27 12:42:46 +0200369 ATOMIC_POST_INC(hfuzz->timeoutedCnt);
370 }
371}
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100372
373void subproc_checkTermination(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
374{
375 if (ATOMIC_GET(hfuzz->terminating)) {
376 LOG_D("Killing PID: %d", (int)fuzzer->pid);
377 kill(fuzzer->pid, SIGKILL);
378 }
379}