blob: 3bf9de757fea61b1ef97c89c837dbd9f8266e628 [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
Jagger93253f72016-09-01 22:40:12 +020029#include <fcntl.h>
Jaggerc7743512016-08-31 22:00:19 +020030#include <inttypes.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020031#include <signal.h>
32#include <stdio.h>
Jaggerc7743512016-08-31 22:00:19 +020033#include <stdlib.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020034#include <string.h>
Jaggerc7743512016-08-31 22:00:19 +020035#include <sys/resource.h>
Jagger93253f72016-09-01 22:40:12 +020036#include <sys/socket.h>
Jaggerc7743512016-08-31 22:00:19 +020037#include <sys/time.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020038#include <sys/types.h>
39#include <sys/wait.h>
Jaggerc7743512016-08-31 22:00:19 +020040#include <unistd.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020041
Jagger93253f72016-09-01 22:40:12 +020042#include "arch.h"
Jaggerc4729592016-09-02 01:52:30 +020043#include "files.h"
Robert Swiecki0e673882016-03-31 18:50:29 +020044#include "log.h"
Jaggerc7743512016-08-31 22:00:19 +020045#include "sancov.h"
46#include "util.h"
47
48extern char **environ;
Robert Swiecki0e673882016-03-31 18:50:29 +020049
50const char *subproc_StatusToStr(int status, char *str, size_t len)
51{
52 if (WIFEXITED(status)) {
53 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
54 return str;
55 }
56
57 if (WIFSIGNALED(status)) {
58 snprintf(str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status),
59 strsignal(WTERMSIG(status)));
60 return str;
61 }
Robert Swiecki0e673882016-03-31 18:50:29 +020062 if (WIFCONTINUED(status)) {
63 snprintf(str, len, "CONTINUED");
64 return str;
65 }
66
67 if (!WIFSTOPPED(status)) {
68 snprintf(str, len, "UNKNOWN STATUS: %d", status);
69 return str;
70 }
71
72 /* Must be in a stopped state */
73 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
74 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
75 strsignal(WSTOPSIG(status)));
76 return str;
77 }
Robert Swiecki2531d752016-04-19 14:00:17 +020078#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020079#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
80 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
81 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swiecki2531d752016-04-19 14:00:17 +020082 case PTRACE_EVENT_FORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020083 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
84 strsignal(WSTOPSIG(status)));
85 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020086 case PTRACE_EVENT_VFORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020087 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
88 strsignal(WSTOPSIG(status)));
89 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020090 case PTRACE_EVENT_CLONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020091 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
92 strsignal(WSTOPSIG(status)));
93 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020094 case PTRACE_EVENT_EXEC:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020095 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
96 strsignal(WSTOPSIG(status)));
97 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020098 case PTRACE_EVENT_VFORK_DONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020099 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
100 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
101 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200102 case PTRACE_EVENT_EXIT:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200103 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
104 strsignal(WSTOPSIG(status)));
105 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200106 case PTRACE_EVENT_SECCOMP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200107 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status),
108 strsignal(WSTOPSIG(status)));
109 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200110 case PTRACE_EVENT_STOP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200111 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki0e673882016-03-31 18:50:29 +0200112 strsignal(WSTOPSIG(status)));
113 return str;
114 default:
115 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
116 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
117 return str;
118 }
119 }
Robert Swiecki2531d752016-04-19 14:00:17 +0200120#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200121
122 snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status),
123 strsignal(WSTOPSIG(status)));
124 return str;
125}
Jaggerc7743512016-08-31 22:00:19 +0200126
127bool subproc_PrepareExecv(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, const char *fileName)
128{
129 /*
Jaggerc7743512016-08-31 22:00:19 +0200130 * The address space limit. If big enough - roughly the size of RAM used
131 */
132 if (hfuzz->asLimit) {
133 struct rlimit rl = {
134 .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL,
135 .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL,
136 };
137 if (setrlimit(RLIMIT_AS, &rl) == -1) {
138 PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
139 }
140 }
141
142 if (hfuzz->nullifyStdio) {
143 util_nullifyStdio();
144 }
145
146 if (hfuzz->fuzzStdin) {
147 /*
148 * Uglyyyyyy ;)
149 */
150 if (!util_redirectStdin(fileName)) {
151 return false;
152 }
153 }
154
155 if (hfuzz->clearEnv) {
156 environ = NULL;
157 }
158 if (sancov_prepareExecve(hfuzz) == false) {
159 LOG_E("sancov_prepareExecve() failed");
160 return false;
161 }
162 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
163 putenv(hfuzz->envs[i]);
164 }
165 char fuzzNo[128];
166 snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, fuzzer->fuzzNo);
167 setenv(_HF_THREAD_NO_ENV, fuzzNo, 1);
168
169 setsid();
170
171 if (hfuzz->bbFd != -1) {
172 if (dup2(hfuzz->bbFd, _HF_BITMAP_FD) == -1) {
173 PLOG_F("dup2('%d', %d)", hfuzz->bbFd, _HF_BITMAP_FD);
174 }
Jaggerae663be2016-09-10 03:27:10 +0200175 close(hfuzz->bbFd);
Jaggerc7743512016-08-31 22:00:19 +0200176 }
177
178 return true;
179}
Jagger93253f72016-09-01 22:40:12 +0200180
Jaggerd1fc9ed2016-09-02 01:06:51 +0200181bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jagger93253f72016-09-01 22:40:12 +0200182{
183 fuzzer->pid = fuzzer->persistentPid;
184 if (fuzzer->pid != 0) {
185 return true;
186 }
187
188 int sv[2];
189 if (hfuzz->persistent) {
Jagger82a20c02016-09-02 01:36:39 +0200190 if (fuzzer->persistentSock != -1) {
191 close(fuzzer->persistentSock);
192 }
193
Jaggere4dad652016-09-10 03:35:50 +0200194 int sock_type = SOCK_STREAM;
195#if defined(SOCK_CLOEXEC)
196 sock_type |= SOCK_CLOEXEC;
197#endif
198 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200199 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
200 return false;
201 }
Robert Swieckic7a95f82016-10-27 19:29:35 +0200202 fuzzer->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200203 }
204
205 fuzzer->pid = arch_fork(hfuzz, fuzzer);
206 if (fuzzer->pid == -1) {
207 PLOG_F("Couldn't fork");
208 }
Robert Swieckia71a4992016-09-02 14:47:55 +0200209 // Child
Jagger93253f72016-09-01 22:40:12 +0200210 if (!fuzzer->pid) {
211 if (hfuzz->persistent) {
212 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
213 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
214 }
215 close(sv[0]);
216 close(sv[1]);
217 }
218
219 if (!subproc_PrepareExecv(hfuzz, fuzzer, fuzzer->fileName)) {
220 LOG_E("subproc_PrepareExecv() failed");
221 exit(EXIT_FAILURE);
222 }
223 if (!arch_launchChild(hfuzz, fuzzer->fileName)) {
224 LOG_E("Error launching child process");
225 exit(EXIT_FAILURE);
226 }
227
228 abort();
229 }
Robert Swieckia71a4992016-09-02 14:47:55 +0200230 // Parent
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300231 LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax);
Jagger68f33592016-09-03 04:52:16 +0200232
Jagger93253f72016-09-01 22:40:12 +0200233 if (hfuzz->persistent) {
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300234 close(sv[1]);
Robert Swiecki2a7da342016-10-27 19:27:30 +0200235 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid);
236 fuzzer->persistentPid = fuzzer->pid;
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300237 }
Jagger93253f72016-09-01 22:40:12 +0200238
239 return true;
240}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200241
Jaggerc4729592016-09-02 01:52:30 +0200242bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
243{
244 if (hfuzz->persistent == false) {
245 return false;
246 }
247 char z;
248 if (recv(fuzzer->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) {
249 LOG_D("Persistent mode round finished");
250 return true;
251 }
252 return false;
253}
254
255static bool subproc_persistentSendFile(fuzzer_t * fuzzer)
256{
257 uint32_t len = (uint64_t) fuzzer->dynamicFileSz;
258 if (files_writeToFd(fuzzer->persistentSock, (uint8_t *) & len, sizeof(len)) == false) {
259 return false;
260 }
261 if (files_writeToFd(fuzzer->persistentSock, fuzzer->dynamicFile, fuzzer->dynamicFileSz) ==
262 false) {
263 return false;
264 }
265 return true;
266}
267
Jaggerd1fc9ed2016-09-02 01:06:51 +0200268void subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
269{
270 arch_prepareChild(hfuzz, fuzzer);
Jaggerc4729592016-09-02 01:52:30 +0200271 if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) {
272 LOG_W("Could not send file contents to the persistent process");
273 }
Jaggerd1fc9ed2016-09-02 01:06:51 +0200274 arch_reapChild(hfuzz, fuzzer);
275}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200276
277uint8_t subproc_System(const char *const argv[])
278{
279 pid_t pid = fork();
280 if (pid == -1) {
281 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200282 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200283 }
284
285 if (!pid) {
286 execv(argv[0], (char *const *)&argv[0]);
287 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200288 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200289 }
290
291 int status;
292 int flags = 0;
293#if defined(__WNOTHREAD)
294 flags |= __WNOTHREAD;
295#endif /* defined(__WNOTHREAD) */
296 while (wait4(pid, &status, flags, NULL) != pid) ;
297 if (WIFSIGNALED(status)) {
298 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
299 return (100 + WTERMSIG(status));
300 }
301 if (!WIFEXITED(status)) {
302 LOG_F("Command '%s' terminated abnormally, status: %d", argv[0], status);
303 return 100;
304 }
305
306 LOG_D("Command '%s' exited with: %d", argv[0], WEXITSTATUS(status));
307
308 if (WEXITSTATUS(status)) {
Robert Swieckicaef4f32016-09-09 18:18:22 +0200309 LOG_W("Command '%s' exited with code: %d", argv[0], WEXITSTATUS(status));
Robert Swiecki54333cd2016-09-07 17:51:48 +0200310 return 1U;
311 }
312
313 return 0U;
314}
Robert Swieckifddc0862016-10-27 12:42:46 +0200315
316void subproc_checkTimeLimit(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
317{
318 if (hfuzz->tmOut == 0) {
319 return;
320 }
321
322 int64_t curMillis = util_timeNowMillis();
323 int64_t diffMillis = curMillis - fuzzer->timeStartedMillis;
324 if (diffMillis > (hfuzz->tmOut * 1000)) {
325 LOG_W("PID %d took too much time (limit %ld s). Sending SIGKILL",
326 fuzzer->pid, hfuzz->tmOut);
327 kill(fuzzer->pid, SIGKILL);
328 ATOMIC_POST_INC(hfuzz->timeoutedCnt);
329 }
330}