Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 1 | /* |
| 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 Swiecki | 2d01690 | 2017-05-24 02:12:30 +0200 | [diff] [blame] | 26 | #include "libcommon/common.h" |
| 27 | |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 28 | #include "subproc.h" |
| 29 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 30 | #include <errno.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 31 | #include <fcntl.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 32 | #include <inttypes.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #include <stdio.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 35 | #include <stdlib.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 36 | #include <string.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 37 | #include <sys/resource.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 38 | #include <sys/socket.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 39 | #include <sys/time.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <sys/wait.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 43 | |
Robert Swiecki | 2d01690 | 2017-05-24 02:12:30 +0200 | [diff] [blame] | 44 | #include "libcommon/arch.h" |
| 45 | #include "libcommon/files.h" |
| 46 | #include "libcommon/log.h" |
Robert Swiecki | 2d01690 | 2017-05-24 02:12:30 +0200 | [diff] [blame] | 47 | #include "libcommon/util.h" |
Robert Swiecki | ec7b845 | 2017-06-01 13:25:56 +0200 | [diff] [blame^] | 48 | #include "sanitizers.h" |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 49 | |
| 50 | extern char **environ; |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 51 | |
| 52 | const 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 Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 64 | 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 Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 80 | #if defined(PTRACE_EVENT_STOP) |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 81 | #define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16) |
| 82 | if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) { |
| 83 | switch (__LINUX_WPTRACEEVENT(status)) { |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 84 | case PTRACE_EVENT_FORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 85 | snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status), |
| 86 | strsignal(WSTOPSIG(status))); |
| 87 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 88 | case PTRACE_EVENT_VFORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 89 | snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status), |
| 90 | strsignal(WSTOPSIG(status))); |
| 91 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 92 | case PTRACE_EVENT_CLONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 93 | snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status), |
| 94 | strsignal(WSTOPSIG(status))); |
| 95 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 96 | case PTRACE_EVENT_EXEC: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 97 | snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status), |
| 98 | strsignal(WSTOPSIG(status))); |
| 99 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 100 | case PTRACE_EVENT_VFORK_DONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 101 | snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)", |
| 102 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 103 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 104 | case PTRACE_EVENT_EXIT: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 105 | snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status), |
| 106 | strsignal(WSTOPSIG(status))); |
| 107 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 108 | case PTRACE_EVENT_SECCOMP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 109 | snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status), |
| 110 | strsignal(WSTOPSIG(status))); |
| 111 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 112 | case PTRACE_EVENT_STOP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 113 | snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status), |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 114 | 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 Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 122 | #endif /* defined(PTRACE_EVENT_STOP) */ |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 123 | |
| 124 | snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), |
| 125 | strsignal(WSTOPSIG(status))); |
| 126 | return str; |
| 127 | } |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 128 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 129 | bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 130 | { |
| 131 | if (hfuzz->persistent == false) { |
| 132 | return false; |
| 133 | } |
Robert Swiecki | bcfdb97 | 2016-11-23 20:33:41 +0100 | [diff] [blame] | 134 | char z; |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 135 | 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 | |
| 142 | static 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 | |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 155 | bool subproc_PrepareExecv(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, const char *fileName) |
| 156 | { |
| 157 | /* |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 158 | * 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 Bechtsoudis | e5f09f8 | 2016-12-27 16:06:05 +0200 | [diff] [blame] | 186 | if (sanitizers_prepareExecve(hfuzz) == false) { |
| 187 | LOG_E("sanitizers_prepareExecve() failed"); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 188 | 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 | } |
Jagger | ae663be | 2016-09-10 03:27:10 +0200 | [diff] [blame] | 203 | close(hfuzz->bbFd); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Robert Swiecki | 491d7bb | 2017-02-28 03:02:53 +0100 | [diff] [blame] | 206 | sigset_t sset; |
| 207 | sigemptyset(&sset); |
| 208 | if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) { |
| 209 | PLOG_W("sigprocmask(empty_set)"); |
| 210 | } |
| 211 | |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 212 | return true; |
| 213 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 214 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 215 | static bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 216 | { |
| 217 | fuzzer->pid = fuzzer->persistentPid; |
| 218 | if (fuzzer->pid != 0) { |
| 219 | return true; |
| 220 | } |
Gergely Nagy | f95cda6 | 2016-12-12 23:40:16 +0100 | [diff] [blame] | 221 | fuzzer->tmOutSignaled = false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 222 | |
| 223 | int sv[2]; |
| 224 | if (hfuzz->persistent) { |
Jagger | 82a20c0 | 2016-09-02 01:36:39 +0200 | [diff] [blame] | 225 | if (fuzzer->persistentSock != -1) { |
| 226 | close(fuzzer->persistentSock); |
| 227 | } |
| 228 | |
Jagger | e4dad65 | 2016-09-10 03:35:50 +0200 | [diff] [blame] | 229 | 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) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 234 | PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)"); |
| 235 | return false; |
| 236 | } |
Robert Swiecki | c7a95f8 | 2016-10-27 19:29:35 +0200 | [diff] [blame] | 237 | fuzzer->persistentSock = sv[0]; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | fuzzer->pid = arch_fork(hfuzz, fuzzer); |
| 241 | if (fuzzer->pid == -1) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 242 | PLOG_E("Couldn't fork"); |
| 243 | return false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 244 | } |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 245 | /* The child process */ |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 246 | if (!fuzzer->pid) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 247 | 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 Swiecki | c337d4f | 2017-06-01 13:09:41 +0200 | [diff] [blame] | 252 | * |
| 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 Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 257 | */ |
| 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 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 267 | 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 Swiecki | a7ecf16 | 2017-02-12 23:17:41 +0100 | [diff] [blame] | 280 | kill(hfuzz->mainPid, SIGTERM); |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 281 | LOG_E("Error launching child process"); |
Robert Swiecki | a375f4b | 2017-06-01 13:20:54 +0200 | [diff] [blame] | 282 | _exit(1); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 283 | } |
Robert Swiecki | 42cc982 | 2017-02-12 22:20:46 +0100 | [diff] [blame] | 284 | abort(); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 285 | } |
Robert Swiecki | c337d4f | 2017-06-01 13:09:41 +0200 | [diff] [blame] | 286 | |
| 287 | /* Parent */ |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 288 | LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax); |
Jagger | 68f3359 | 2016-09-03 04:52:16 +0200 | [diff] [blame] | 289 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 290 | if (hfuzz->persistent) { |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 291 | close(sv[1]); |
Robert Swiecki | 2a7da34 | 2016-10-27 19:27:30 +0200 | [diff] [blame] | 292 | LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid); |
| 293 | fuzzer->persistentPid = fuzzer->pid; |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 294 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 295 | |
| 296 | return true; |
| 297 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 298 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 299 | bool subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 300 | { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 301 | if (subproc_New(hfuzz, fuzzer) == false) { |
| 302 | LOG_E("subproc_New()"); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 303 | return false; |
| 304 | } |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 305 | |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 306 | arch_prepareChild(hfuzz, fuzzer); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 307 | if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) { |
| 308 | LOG_W("Could not send file contents to the persistent process"); |
| 309 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 310 | arch_reapChild(hfuzz, fuzzer); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 311 | |
| 312 | return true; |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 313 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 314 | |
| 315 | uint8_t subproc_System(const char *const argv[]) |
| 316 | { |
| 317 | pid_t pid = fork(); |
| 318 | if (pid == -1) { |
| 319 | PLOG_E("Couldn't fork"); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 320 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (!pid) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 324 | logMutexReset(); |
Robert Swiecki | 491d7bb | 2017-02-28 03:02:53 +0100 | [diff] [blame] | 325 | sigset_t sset; |
| 326 | sigemptyset(&sset); |
| 327 | sigprocmask(SIG_SETMASK, &sset, NULL); |
Robert Swiecki | 87ca211 | 2017-04-27 12:03:53 +0200 | [diff] [blame] | 328 | setsid(); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 329 | execv(argv[0], (char *const *)&argv[0]); |
| 330 | PLOG_F("Couldn't execute '%s'", argv[0]); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 331 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | int status; |
| 335 | int flags = 0; |
| 336 | #if defined(__WNOTHREAD) |
| 337 | flags |= __WNOTHREAD; |
| 338 | #endif /* defined(__WNOTHREAD) */ |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 339 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 340 | 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 Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 364 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 365 | LOG_D("wait4() returned with status: %d", status); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 366 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 367 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 368 | |
| 369 | void 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 Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 378 | /* 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 Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 387 | 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 Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 393 | ATOMIC_POST_INC(hfuzz->timeoutedCnt); |
| 394 | } |
| 395 | } |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 396 | |
| 397 | void 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 | } |