blob: a267c86409e24a3a95709ea977a30db360f4369f [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 /*
130 * Set timeout (prof), real timeout (2*prof), and rlimit_cpu (2*prof)
131 */
132 if (hfuzz->persistent == false && hfuzz->tmOut) {
133 struct itimerval it;
134
135 /*
136 * The hfuzz->tmOut is real CPU usage time...
137 */
138 it.it_value.tv_sec = hfuzz->tmOut;
139 it.it_value.tv_usec = 0;
140 it.it_interval.tv_sec = 0;
141 it.it_interval.tv_usec = 0;
142 if (setitimer(ITIMER_PROF, &it, NULL) == -1) {
143 PLOG_D("Couldn't set the ITIMER_PROF timer");
144 }
145
146 /*
147 * ...so, if a process sleeps, this one should
148 * trigger a signal...
149 */
150 it.it_value.tv_sec = hfuzz->tmOut;
151 it.it_value.tv_usec = 0;
152 it.it_interval.tv_sec = 0;
153 it.it_interval.tv_usec = 0;
154 if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
155 PLOG_E("Couldn't set the ITIMER_REAL timer");
156 return false;
157 }
158
159 /*
160 * ..if a process sleeps and catches SIGPROF/SIGALRM
161 * rlimits won't help either. However, arch_checkTimeLimit
162 * will send a SIGKILL at tmOut + 2 seconds. That should
163 * do it :)
164 */
165 struct rlimit rl;
166
167 rl.rlim_cur = hfuzz->tmOut + 1;
168 rl.rlim_max = hfuzz->tmOut + 1;
169 if (setrlimit(RLIMIT_CPU, &rl) == -1) {
170 PLOG_D("Couldn't enforce the RLIMIT_CPU resource limit");
171 }
172 }
173
174 /*
175 * The address space limit. If big enough - roughly the size of RAM used
176 */
177 if (hfuzz->asLimit) {
178 struct rlimit rl = {
179 .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL,
180 .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL,
181 };
182 if (setrlimit(RLIMIT_AS, &rl) == -1) {
183 PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
184 }
185 }
186
187 if (hfuzz->nullifyStdio) {
188 util_nullifyStdio();
189 }
190
191 if (hfuzz->fuzzStdin) {
192 /*
193 * Uglyyyyyy ;)
194 */
195 if (!util_redirectStdin(fileName)) {
196 return false;
197 }
198 }
199
200 if (hfuzz->clearEnv) {
201 environ = NULL;
202 }
203 if (sancov_prepareExecve(hfuzz) == false) {
204 LOG_E("sancov_prepareExecve() failed");
205 return false;
206 }
207 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
208 putenv(hfuzz->envs[i]);
209 }
210 char fuzzNo[128];
211 snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, fuzzer->fuzzNo);
212 setenv(_HF_THREAD_NO_ENV, fuzzNo, 1);
213
214 setsid();
215
216 if (hfuzz->bbFd != -1) {
217 if (dup2(hfuzz->bbFd, _HF_BITMAP_FD) == -1) {
218 PLOG_F("dup2('%d', %d)", hfuzz->bbFd, _HF_BITMAP_FD);
219 }
Jaggerae663be2016-09-10 03:27:10 +0200220 close(hfuzz->bbFd);
Jaggerc7743512016-08-31 22:00:19 +0200221 }
222
223 return true;
224}
Jagger93253f72016-09-01 22:40:12 +0200225
Jaggerd1fc9ed2016-09-02 01:06:51 +0200226bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
Jagger93253f72016-09-01 22:40:12 +0200227{
228 fuzzer->pid = fuzzer->persistentPid;
229 if (fuzzer->pid != 0) {
230 return true;
231 }
232
233 int sv[2];
234 if (hfuzz->persistent) {
Jagger82a20c02016-09-02 01:36:39 +0200235 if (fuzzer->persistentSock != -1) {
236 close(fuzzer->persistentSock);
237 }
238
Jaggere4dad652016-09-10 03:35:50 +0200239 int sock_type = SOCK_STREAM;
240#if defined(SOCK_CLOEXEC)
241 sock_type |= SOCK_CLOEXEC;
242#endif
243 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200244 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
245 return false;
246 }
Jagger48b75a92016-09-03 20:33:57 +0200247 fuzzer->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200248 }
249
250 fuzzer->pid = arch_fork(hfuzz, fuzzer);
251 if (fuzzer->pid == -1) {
252 PLOG_F("Couldn't fork");
253 }
Robert Swieckia71a4992016-09-02 14:47:55 +0200254 // Child
Jagger93253f72016-09-01 22:40:12 +0200255 if (!fuzzer->pid) {
256 if (hfuzz->persistent) {
257 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
258 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
259 }
260 close(sv[0]);
261 close(sv[1]);
262 }
263
264 if (!subproc_PrepareExecv(hfuzz, fuzzer, fuzzer->fileName)) {
265 LOG_E("subproc_PrepareExecv() failed");
266 exit(EXIT_FAILURE);
267 }
268 if (!arch_launchChild(hfuzz, fuzzer->fileName)) {
269 LOG_E("Error launching child process");
270 exit(EXIT_FAILURE);
271 }
272
273 abort();
274 }
Robert Swieckia71a4992016-09-02 14:47:55 +0200275 // Parent
276 if (hfuzz->persistent == false) {
Jagger64b8e362016-09-03 01:21:42 +0200277 return true;
Robert Swieckia71a4992016-09-02 14:47:55 +0200278 }
279
Jagger93253f72016-09-01 22:40:12 +0200280 close(sv[1]);
Jagger68f33592016-09-03 04:52:16 +0200281
Jagger93253f72016-09-01 22:40:12 +0200282 if (hfuzz->persistent) {
283 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid);
284 fuzzer->persistentPid = fuzzer->pid;
285 }
286
287 LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax);
288
289 return true;
290}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200291
Jaggerc4729592016-09-02 01:52:30 +0200292bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
293{
294 if (hfuzz->persistent == false) {
295 return false;
296 }
297 char z;
298 if (recv(fuzzer->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) {
299 LOG_D("Persistent mode round finished");
300 return true;
301 }
302 return false;
303}
304
305static bool subproc_persistentSendFile(fuzzer_t * fuzzer)
306{
307 uint32_t len = (uint64_t) fuzzer->dynamicFileSz;
308 if (files_writeToFd(fuzzer->persistentSock, (uint8_t *) & len, sizeof(len)) == false) {
309 return false;
310 }
311 if (files_writeToFd(fuzzer->persistentSock, fuzzer->dynamicFile, fuzzer->dynamicFileSz) ==
312 false) {
313 return false;
314 }
315 return true;
316}
317
Jaggerd1fc9ed2016-09-02 01:06:51 +0200318void subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
319{
320 arch_prepareChild(hfuzz, fuzzer);
Jaggerc4729592016-09-02 01:52:30 +0200321 if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) {
322 LOG_W("Could not send file contents to the persistent process");
323 }
Jaggerd1fc9ed2016-09-02 01:06:51 +0200324 arch_reapChild(hfuzz, fuzzer);
325}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200326
327uint8_t subproc_System(const char *const argv[])
328{
329 pid_t pid = fork();
330 if (pid == -1) {
331 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200332 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200333 }
334
335 if (!pid) {
336 execv(argv[0], (char *const *)&argv[0]);
337 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200338 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200339 }
340
341 int status;
342 int flags = 0;
343#if defined(__WNOTHREAD)
344 flags |= __WNOTHREAD;
345#endif /* defined(__WNOTHREAD) */
346 while (wait4(pid, &status, flags, NULL) != pid) ;
347 if (WIFSIGNALED(status)) {
348 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
349 return (100 + WTERMSIG(status));
350 }
351 if (!WIFEXITED(status)) {
352 LOG_F("Command '%s' terminated abnormally, status: %d", argv[0], status);
353 return 100;
354 }
355
356 LOG_D("Command '%s' exited with: %d", argv[0], WEXITSTATUS(status));
357
358 if (WEXITSTATUS(status)) {
Robert Swieckicaef4f32016-09-09 18:18:22 +0200359 LOG_W("Command '%s' exited with code: %d", argv[0], WEXITSTATUS(status));
Robert Swiecki54333cd2016-09-07 17:51:48 +0200360 return 1U;
361 }
362
363 return 0U;
364}