blob: 188cf4e6060ab8583818f0d90f72efaa896e4e64 [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
Robert Swiecki2d016902017-05-24 02:12:30 +020026#include "libcommon/common.h"
27
Robert Swiecki0e673882016-03-31 18:50:29 +020028#include "subproc.h"
29
Robert Swiecki1ddf6742017-05-08 21:17:32 +020030#include <errno.h>
Jagger93253f72016-09-01 22:40:12 +020031#include <fcntl.h>
Jaggerc7743512016-08-31 22:00:19 +020032#include <inttypes.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020033#include <signal.h>
34#include <stdio.h>
Jaggerc7743512016-08-31 22:00:19 +020035#include <stdlib.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020036#include <string.h>
Jaggerc7743512016-08-31 22:00:19 +020037#include <sys/resource.h>
Jagger93253f72016-09-01 22:40:12 +020038#include <sys/socket.h>
Jaggerc7743512016-08-31 22:00:19 +020039#include <sys/time.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020040#include <sys/types.h>
41#include <sys/wait.h>
Jaggerc7743512016-08-31 22:00:19 +020042#include <unistd.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020043
Robert Swiecki2d016902017-05-24 02:12:30 +020044#include "libcommon/arch.h"
45#include "libcommon/files.h"
46#include "libcommon/log.h"
Robert Swiecki2d016902017-05-24 02:12:30 +020047#include "libcommon/util.h"
Robert Swieckiec7b8452017-06-01 13:25:56 +020048#include "sanitizers.h"
Jaggerc7743512016-08-31 22:00:19 +020049
50extern char **environ;
Robert Swiecki0e673882016-03-31 18:50:29 +020051
52const char *subproc_StatusToStr(int status, char *str, size_t len)
53{
54 if (WIFEXITED(status)) {
55 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
56 return str;
57 }
58
59 if (WIFSIGNALED(status)) {
60 snprintf(str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status),
61 strsignal(WTERMSIG(status)));
62 return str;
63 }
Robert Swiecki0e673882016-03-31 18:50:29 +020064 if (WIFCONTINUED(status)) {
65 snprintf(str, len, "CONTINUED");
66 return str;
67 }
68
69 if (!WIFSTOPPED(status)) {
70 snprintf(str, len, "UNKNOWN STATUS: %d", status);
71 return str;
72 }
73
74 /* Must be in a stopped state */
75 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
76 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
77 strsignal(WSTOPSIG(status)));
78 return str;
79 }
Robert Swiecki2531d752016-04-19 14:00:17 +020080#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020081#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
82 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
83 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swiecki2531d752016-04-19 14:00:17 +020084 case PTRACE_EVENT_FORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020085 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
86 strsignal(WSTOPSIG(status)));
87 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020088 case PTRACE_EVENT_VFORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020089 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
90 strsignal(WSTOPSIG(status)));
91 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020092 case PTRACE_EVENT_CLONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020093 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
94 strsignal(WSTOPSIG(status)));
95 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020096 case PTRACE_EVENT_EXEC:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020097 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
98 strsignal(WSTOPSIG(status)));
99 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200100 case PTRACE_EVENT_VFORK_DONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200101 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
102 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
103 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200104 case PTRACE_EVENT_EXIT:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200105 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
106 strsignal(WSTOPSIG(status)));
107 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200108 case PTRACE_EVENT_SECCOMP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200109 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status),
110 strsignal(WSTOPSIG(status)));
111 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200112 case PTRACE_EVENT_STOP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200113 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki0e673882016-03-31 18:50:29 +0200114 strsignal(WSTOPSIG(status)));
115 return str;
116 default:
117 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
118 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
119 return str;
120 }
121 }
Robert Swiecki2531d752016-04-19 14:00:17 +0200122#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200123
124 snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status),
125 strsignal(WSTOPSIG(status)));
126 return str;
127}
Jaggerc7743512016-08-31 22:00:19 +0200128
Robert Swiecki940a0802016-11-02 12:42:09 +0100129bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
130{
131 if (hfuzz->persistent == false) {
132 return false;
133 }
Robert Swieckibcfdb972016-11-23 20:33:41 +0100134 char z;
Robert Swiecki940a0802016-11-02 12:42:09 +0100135 if (recv(fuzzer->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) {
136 LOG_D("Persistent mode round finished");
137 return true;
138 }
139 return false;
140}
141
142static bool subproc_persistentSendFile(fuzzer_t * fuzzer)
143{
144 uint32_t len = (uint64_t) fuzzer->dynamicFileSz;
145 if (files_writeToFd(fuzzer->persistentSock, (uint8_t *) & len, sizeof(len)) == false) {
146 return false;
147 }
148 if (files_writeToFd(fuzzer->persistentSock, fuzzer->dynamicFile, fuzzer->dynamicFileSz) ==
149 false) {
150 return false;
151 }
152 return true;
153}
154
Jaggerc7743512016-08-31 22:00:19 +0200155bool subproc_PrepareExecv(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, const char *fileName)
156{
157 /*
Jaggerc7743512016-08-31 22:00:19 +0200158 * The address space limit. If big enough - roughly the size of RAM used
159 */
160 if (hfuzz->asLimit) {
161 struct rlimit rl = {
162 .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL,
163 .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL,
164 };
165 if (setrlimit(RLIMIT_AS, &rl) == -1) {
166 PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
167 }
168 }
169
170 if (hfuzz->nullifyStdio) {
171 util_nullifyStdio();
172 }
173
174 if (hfuzz->fuzzStdin) {
175 /*
176 * Uglyyyyyy ;)
177 */
178 if (!util_redirectStdin(fileName)) {
179 return false;
180 }
181 }
182
183 if (hfuzz->clearEnv) {
184 environ = NULL;
185 }
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200186 if (sanitizers_prepareExecve(hfuzz) == false) {
187 LOG_E("sanitizers_prepareExecve() failed");
Jaggerc7743512016-08-31 22:00:19 +0200188 return false;
189 }
190 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
191 putenv(hfuzz->envs[i]);
192 }
193 char fuzzNo[128];
194 snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, fuzzer->fuzzNo);
195 setenv(_HF_THREAD_NO_ENV, fuzzNo, 1);
196
197 setsid();
198
199 if (hfuzz->bbFd != -1) {
200 if (dup2(hfuzz->bbFd, _HF_BITMAP_FD) == -1) {
201 PLOG_F("dup2('%d', %d)", hfuzz->bbFd, _HF_BITMAP_FD);
202 }
Jaggerae663be2016-09-10 03:27:10 +0200203 close(hfuzz->bbFd);
Jaggerc7743512016-08-31 22:00:19 +0200204 }
205
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100206 sigset_t sset;
207 sigemptyset(&sset);
208 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
209 PLOG_W("sigprocmask(empty_set)");
210 }
211
Jaggerc7743512016-08-31 22:00:19 +0200212 return true;
213}
Jagger93253f72016-09-01 22:40:12 +0200214
Robert Swiecki940a0802016-11-02 12:42:09 +0100215static bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jagger93253f72016-09-01 22:40:12 +0200216{
217 fuzzer->pid = fuzzer->persistentPid;
218 if (fuzzer->pid != 0) {
219 return true;
220 }
Gergely Nagyf95cda62016-12-12 23:40:16 +0100221 fuzzer->tmOutSignaled = false;
Jagger93253f72016-09-01 22:40:12 +0200222
223 int sv[2];
224 if (hfuzz->persistent) {
Jagger82a20c02016-09-02 01:36:39 +0200225 if (fuzzer->persistentSock != -1) {
226 close(fuzzer->persistentSock);
227 }
228
Jaggere4dad652016-09-10 03:35:50 +0200229 int sock_type = SOCK_STREAM;
230#if defined(SOCK_CLOEXEC)
231 sock_type |= SOCK_CLOEXEC;
232#endif
233 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200234 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
235 return false;
236 }
Robert Swieckic7a95f82016-10-27 19:29:35 +0200237 fuzzer->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200238 }
239
240 fuzzer->pid = arch_fork(hfuzz, fuzzer);
241 if (fuzzer->pid == -1) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200242 PLOG_E("Couldn't fork");
243 return false;
Jagger93253f72016-09-01 22:40:12 +0200244 }
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200245 /* The child process */
Jagger93253f72016-09-01 22:40:12 +0200246 if (!fuzzer->pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200247 logMutexReset();
248 /*
249 * Reset sighandlers, and set alarm(1). It's a guarantee against dead-locks
250 * in the child, where we ensure here that the child process will either
251 * execve or get signaled by SIGALRM within 1 second.
Robert Swieckic337d4f2017-06-01 13:09:41 +0200252 *
253 * Those deadlocks typically stem from the fact, that malloc() can behave weirdly
254 * when fork()-ing a single thread of a process: e.g. with glibc < 2.24
255 * (or, Ubuntu's 2.23-0ubuntu6). For more see
256 * http://changelogs.ubuntu.com/changelogs/pool/main/g/glibc/glibc_2.23-0ubuntu7/changelog
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200257 */
258 alarm(1);
259 signal(SIGALRM, SIG_DFL);
260 sigset_t sset;
261 sigemptyset(&sset);
262 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
263 perror("sigprocmask");
264 _exit(1);
265 }
266
Jagger93253f72016-09-01 22:40:12 +0200267 if (hfuzz->persistent) {
268 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
269 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
270 }
271 close(sv[0]);
272 close(sv[1]);
273 }
274
275 if (!subproc_PrepareExecv(hfuzz, fuzzer, fuzzer->fileName)) {
276 LOG_E("subproc_PrepareExecv() failed");
277 exit(EXIT_FAILURE);
278 }
279 if (!arch_launchChild(hfuzz, fuzzer->fileName)) {
Robert Swieckia7ecf162017-02-12 23:17:41 +0100280 kill(hfuzz->mainPid, SIGTERM);
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200281 LOG_E("Error launching child process");
Robert Swieckia375f4b2017-06-01 13:20:54 +0200282 _exit(1);
Jagger93253f72016-09-01 22:40:12 +0200283 }
Robert Swiecki42cc9822017-02-12 22:20:46 +0100284 abort();
Jagger93253f72016-09-01 22:40:12 +0200285 }
Robert Swieckic337d4f2017-06-01 13:09:41 +0200286
287 /* Parent */
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300288 LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax);
Jagger68f33592016-09-03 04:52:16 +0200289
Jagger93253f72016-09-01 22:40:12 +0200290 if (hfuzz->persistent) {
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300291 close(sv[1]);
Robert Swiecki2a7da342016-10-27 19:27:30 +0200292 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid);
293 fuzzer->persistentPid = fuzzer->pid;
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300294 }
Jagger93253f72016-09-01 22:40:12 +0200295
296 return true;
297}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200298
Robert Swiecki940a0802016-11-02 12:42:09 +0100299bool subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jaggerc4729592016-09-02 01:52:30 +0200300{
Robert Swiecki940a0802016-11-02 12:42:09 +0100301 if (subproc_New(hfuzz, fuzzer) == false) {
302 LOG_E("subproc_New()");
Jaggerc4729592016-09-02 01:52:30 +0200303 return false;
304 }
Jaggerc4729592016-09-02 01:52:30 +0200305
Jaggerd1fc9ed2016-09-02 01:06:51 +0200306 arch_prepareChild(hfuzz, fuzzer);
Jaggerc4729592016-09-02 01:52:30 +0200307 if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) {
308 LOG_W("Could not send file contents to the persistent process");
309 }
Jaggerd1fc9ed2016-09-02 01:06:51 +0200310 arch_reapChild(hfuzz, fuzzer);
Robert Swiecki940a0802016-11-02 12:42:09 +0100311
312 return true;
Jaggerd1fc9ed2016-09-02 01:06:51 +0200313}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200314
315uint8_t subproc_System(const char *const argv[])
316{
317 pid_t pid = fork();
318 if (pid == -1) {
319 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200320 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200321 }
322
323 if (!pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200324 logMutexReset();
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100325 sigset_t sset;
326 sigemptyset(&sset);
327 sigprocmask(SIG_SETMASK, &sset, NULL);
Robert Swiecki87ca2112017-04-27 12:03:53 +0200328 setsid();
Robert Swiecki54333cd2016-09-07 17:51:48 +0200329 execv(argv[0], (char *const *)&argv[0]);
330 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200331 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200332 }
333
334 int status;
335 int flags = 0;
336#if defined(__WNOTHREAD)
337 flags |= __WNOTHREAD;
338#endif /* defined(__WNOTHREAD) */
Robert Swiecki54333cd2016-09-07 17:51:48 +0200339
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200340 for (;;) {
341 int ret = wait4(pid, &status, flags, NULL);
342 if (ret == -1 && errno == EINTR) {
343 continue;
344 }
345 if (ret == -1) {
346 PLOG_E("wait4() for process PID: %d", (int)pid);
347 return 255;
348 }
349 if (ret != pid) {
350 LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid);
351 return 255;
352 }
353 if (WIFSIGNALED(status)) {
354 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
355 return (100 + WTERMSIG(status));
356 }
357 if (WIFEXITED(status)) {
358 if (WEXITSTATUS(status) == 0) {
359 return 0U;
360 }
361 LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status));
362 return 1U;
363 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200364
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200365 LOG_D("wait4() returned with status: %d", status);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200366 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200367}
Robert Swieckifddc0862016-10-27 12:42:46 +0200368
369void subproc_checkTimeLimit(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
370{
371 if (hfuzz->tmOut == 0) {
372 return;
373 }
374
375 int64_t curMillis = util_timeNowMillis();
376 int64_t diffMillis = curMillis - fuzzer->timeStartedMillis;
377 if (diffMillis > (hfuzz->tmOut * 1000)) {
Robert Swiecki013bc9c2016-12-12 17:31:06 +0100378 /* Has this instance been already signaled due to timeout? Just, SIGKILL it */
379 if (fuzzer->tmOutSignaled) {
380 LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL",
381 fuzzer->pid);
382 kill(fuzzer->pid, SIGKILL);
383 return;
384 }
385 fuzzer->tmOutSignaled = true;
386
Robert Swieckie84b6452016-12-12 12:42:04 +0100387 LOG_W("PID %d took too much time (limit %ld s). Killing it", fuzzer->pid, hfuzz->tmOut);
388 if (hfuzz->tmout_vtalrm) {
389 kill(fuzzer->pid, SIGVTALRM);
390 } else {
391 kill(fuzzer->pid, SIGKILL);
392 }
Robert Swieckifddc0862016-10-27 12:42:46 +0200393 ATOMIC_POST_INC(hfuzz->timeoutedCnt);
394 }
395}
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100396
397void subproc_checkTermination(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
398{
399 if (ATOMIC_GET(hfuzz->terminating)) {
400 LOG_D("Killing PID: %d", (int)fuzzer->pid);
401 kill(fuzzer->pid, SIGKILL);
402 }
403}