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 | |
| 26 | #include "common.h" |
| 27 | #include "subproc.h" |
| 28 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame^] | 29 | #include <errno.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 30 | #include <fcntl.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 31 | #include <inttypes.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 32 | #include <signal.h> |
| 33 | #include <stdio.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 34 | #include <stdlib.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 35 | #include <string.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 36 | #include <sys/resource.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 37 | #include <sys/socket.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 38 | #include <sys/time.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/wait.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 41 | #include <unistd.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 42 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 43 | #include "arch.h" |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 44 | #include "files.h" |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 45 | #include "log.h" |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 46 | #include "sancov.h" |
| 47 | #include "util.h" |
| 48 | |
| 49 | extern char **environ; |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 50 | |
| 51 | const 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 Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 63 | 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 Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 79 | #if defined(PTRACE_EVENT_STOP) |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 80 | #define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16) |
| 81 | if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) { |
| 82 | switch (__LINUX_WPTRACEEVENT(status)) { |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 83 | case PTRACE_EVENT_FORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 84 | snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status), |
| 85 | strsignal(WSTOPSIG(status))); |
| 86 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 87 | case PTRACE_EVENT_VFORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 88 | snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status), |
| 89 | strsignal(WSTOPSIG(status))); |
| 90 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 91 | case PTRACE_EVENT_CLONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 92 | snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status), |
| 93 | strsignal(WSTOPSIG(status))); |
| 94 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 95 | case PTRACE_EVENT_EXEC: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 96 | snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status), |
| 97 | strsignal(WSTOPSIG(status))); |
| 98 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 99 | case PTRACE_EVENT_VFORK_DONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 100 | snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)", |
| 101 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 102 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 103 | case PTRACE_EVENT_EXIT: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 104 | snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status), |
| 105 | strsignal(WSTOPSIG(status))); |
| 106 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 107 | case PTRACE_EVENT_SECCOMP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 108 | snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status), |
| 109 | strsignal(WSTOPSIG(status))); |
| 110 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 111 | case PTRACE_EVENT_STOP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 112 | snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status), |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 113 | 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 Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 121 | #endif /* defined(PTRACE_EVENT_STOP) */ |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 122 | |
| 123 | snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), |
| 124 | strsignal(WSTOPSIG(status))); |
| 125 | return str; |
| 126 | } |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 127 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 128 | bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 129 | { |
| 130 | if (hfuzz->persistent == false) { |
| 131 | return false; |
| 132 | } |
Robert Swiecki | bcfdb97 | 2016-11-23 20:33:41 +0100 | [diff] [blame] | 133 | char z; |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 134 | 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 | |
| 141 | static 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 | |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 154 | bool subproc_PrepareExecv(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, const char *fileName) |
| 155 | { |
| 156 | /* |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 157 | * 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 Bechtsoudis | e5f09f8 | 2016-12-27 16:06:05 +0200 | [diff] [blame] | 185 | if (sanitizers_prepareExecve(hfuzz) == false) { |
| 186 | LOG_E("sanitizers_prepareExecve() failed"); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 187 | 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 | } |
Jagger | ae663be | 2016-09-10 03:27:10 +0200 | [diff] [blame] | 202 | close(hfuzz->bbFd); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Robert Swiecki | 491d7bb | 2017-02-28 03:02:53 +0100 | [diff] [blame] | 205 | sigset_t sset; |
| 206 | sigemptyset(&sset); |
| 207 | if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) { |
| 208 | PLOG_W("sigprocmask(empty_set)"); |
| 209 | } |
| 210 | |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 211 | return true; |
| 212 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 213 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 214 | static bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 215 | { |
| 216 | fuzzer->pid = fuzzer->persistentPid; |
| 217 | if (fuzzer->pid != 0) { |
| 218 | return true; |
| 219 | } |
Gergely Nagy | f95cda6 | 2016-12-12 23:40:16 +0100 | [diff] [blame] | 220 | fuzzer->tmOutSignaled = false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 221 | |
| 222 | int sv[2]; |
| 223 | if (hfuzz->persistent) { |
Jagger | 82a20c0 | 2016-09-02 01:36:39 +0200 | [diff] [blame] | 224 | if (fuzzer->persistentSock != -1) { |
| 225 | close(fuzzer->persistentSock); |
| 226 | } |
| 227 | |
Jagger | e4dad65 | 2016-09-10 03:35:50 +0200 | [diff] [blame] | 228 | 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) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 233 | PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)"); |
| 234 | return false; |
| 235 | } |
Robert Swiecki | c7a95f8 | 2016-10-27 19:29:35 +0200 | [diff] [blame] | 236 | fuzzer->persistentSock = sv[0]; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | fuzzer->pid = arch_fork(hfuzz, fuzzer); |
| 240 | if (fuzzer->pid == -1) { |
| 241 | PLOG_F("Couldn't fork"); |
| 242 | } |
Robert Swiecki | a71a499 | 2016-09-02 14:47:55 +0200 | [diff] [blame] | 243 | // Child |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 244 | 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 Swiecki | a7ecf16 | 2017-02-12 23:17:41 +0100 | [diff] [blame] | 259 | kill(hfuzz->mainPid, SIGTERM); |
Robert Swiecki | 9907100 | 2017-02-13 17:16:18 +0100 | [diff] [blame] | 260 | exit(EXIT_FAILURE); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 261 | } |
Robert Swiecki | 42cc982 | 2017-02-12 22:20:46 +0100 | [diff] [blame] | 262 | abort(); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 263 | } |
Robert Swiecki | a71a499 | 2016-09-02 14:47:55 +0200 | [diff] [blame] | 264 | // Parent |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 265 | LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax); |
Jagger | 68f3359 | 2016-09-03 04:52:16 +0200 | [diff] [blame] | 266 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 267 | if (hfuzz->persistent) { |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 268 | close(sv[1]); |
Robert Swiecki | 2a7da34 | 2016-10-27 19:27:30 +0200 | [diff] [blame] | 269 | LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid); |
| 270 | fuzzer->persistentPid = fuzzer->pid; |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 271 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 272 | |
| 273 | return true; |
| 274 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 275 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 276 | bool subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 277 | { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 278 | if (subproc_New(hfuzz, fuzzer) == false) { |
| 279 | LOG_E("subproc_New()"); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 280 | return false; |
| 281 | } |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 282 | |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 283 | arch_prepareChild(hfuzz, fuzzer); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 284 | if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) { |
| 285 | LOG_W("Could not send file contents to the persistent process"); |
| 286 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 287 | arch_reapChild(hfuzz, fuzzer); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 288 | |
| 289 | return true; |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 290 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 291 | |
| 292 | uint8_t subproc_System(const char *const argv[]) |
| 293 | { |
| 294 | pid_t pid = fork(); |
| 295 | if (pid == -1) { |
| 296 | PLOG_E("Couldn't fork"); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 297 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (!pid) { |
Robert Swiecki | 491d7bb | 2017-02-28 03:02:53 +0100 | [diff] [blame] | 301 | sigset_t sset; |
| 302 | sigemptyset(&sset); |
| 303 | sigprocmask(SIG_SETMASK, &sset, NULL); |
Robert Swiecki | 87ca211 | 2017-04-27 12:03:53 +0200 | [diff] [blame] | 304 | setsid(); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 305 | execv(argv[0], (char *const *)&argv[0]); |
| 306 | PLOG_F("Couldn't execute '%s'", argv[0]); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 307 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | int status; |
| 311 | int flags = 0; |
| 312 | #if defined(__WNOTHREAD) |
| 313 | flags |= __WNOTHREAD; |
| 314 | #endif /* defined(__WNOTHREAD) */ |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 315 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame^] | 316 | 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 Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 340 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame^] | 341 | LOG_D("wait4() returned with status: %d", status); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 342 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 343 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 344 | |
| 345 | void 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 Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 354 | /* 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 Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 363 | 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 Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 369 | ATOMIC_POST_INC(hfuzz->timeoutedCnt); |
| 370 | } |
| 371 | } |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 372 | |
| 373 | void 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 | } |