Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * honggfuzz - routines dealing with subprocesses |
| 4 | * ----------------------------------------- |
| 5 | * |
Robert Swiecki | 46288f7 | 2018-02-27 17:28:47 +0100 | [diff] [blame] | 6 | * Author: Robert Swiecki <swiecki@google.com> |
| 7 | * Felix Gröbert <groebert@google.com> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 8 | * |
Robert Swiecki | 46288f7 | 2018-02-27 17:28:47 +0100 | [diff] [blame] | 9 | * Copyright 2010-2018 by Google Inc. All Rights Reserved. |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. You may obtain |
| 13 | * a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 20 | * implied. See the License for the specific language governing |
| 21 | * permissions and limitations under the License. |
| 22 | * |
| 23 | */ |
| 24 | |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 25 | #include "subproc.h" |
| 26 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 27 | #include <errno.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 28 | #include <fcntl.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 29 | #include <inttypes.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <stdio.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 32 | #include <stdlib.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 33 | #include <string.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 34 | #include <sys/resource.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 35 | #include <sys/socket.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 36 | #include <sys/time.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 37 | #include <sys/types.h> |
| 38 | #include <sys/wait.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 39 | #include <unistd.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 40 | |
Robert Swiecki | d0fa62c | 2017-09-28 18:11:05 +0200 | [diff] [blame] | 41 | #include "arch.h" |
Robert Swiecki | 35978ac | 2017-11-16 18:00:53 +0100 | [diff] [blame] | 42 | #include "fuzz.h" |
Robert Swiecki | 246af3e | 2018-01-05 14:56:32 +0100 | [diff] [blame] | 43 | #include "libhfcommon/common.h" |
| 44 | #include "libhfcommon/files.h" |
| 45 | #include "libhfcommon/log.h" |
| 46 | #include "libhfcommon/util.h" |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 47 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 48 | extern char** environ; |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 49 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 50 | const char* subproc_StatusToStr(int status, char* str, size_t len) { |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 51 | if (WIFEXITED(status)) { |
| 52 | snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status)); |
| 53 | return str; |
| 54 | } |
| 55 | |
| 56 | if (WIFSIGNALED(status)) { |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 57 | snprintf( |
| 58 | str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status), strsignal(WTERMSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 59 | return str; |
| 60 | } |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 61 | if (WIFCONTINUED(status)) { |
| 62 | snprintf(str, len, "CONTINUED"); |
| 63 | return str; |
| 64 | } |
| 65 | |
| 66 | if (!WIFSTOPPED(status)) { |
| 67 | snprintf(str, len, "UNKNOWN STATUS: %d", status); |
| 68 | return str; |
| 69 | } |
| 70 | |
| 71 | /* Must be in a stopped state */ |
| 72 | if (WSTOPSIG(status) == (SIGTRAP | 0x80)) { |
| 73 | snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 74 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 75 | return str; |
| 76 | } |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 77 | #if defined(PTRACE_EVENT_STOP) |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 78 | #define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16) |
| 79 | if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) { |
| 80 | switch (__LINUX_WPTRACEEVENT(status)) { |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 81 | case PTRACE_EVENT_FORK: |
| 82 | snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status), |
| 83 | strsignal(WSTOPSIG(status))); |
| 84 | return str; |
| 85 | case PTRACE_EVENT_VFORK: |
| 86 | snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status), |
| 87 | strsignal(WSTOPSIG(status))); |
| 88 | return str; |
| 89 | case PTRACE_EVENT_CLONE: |
| 90 | snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status), |
| 91 | strsignal(WSTOPSIG(status))); |
| 92 | return str; |
| 93 | case PTRACE_EVENT_EXEC: |
| 94 | snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status), |
| 95 | strsignal(WSTOPSIG(status))); |
| 96 | return str; |
| 97 | case PTRACE_EVENT_VFORK_DONE: |
| 98 | snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)", |
| 99 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 100 | return str; |
| 101 | case PTRACE_EVENT_EXIT: |
| 102 | snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status), |
| 103 | strsignal(WSTOPSIG(status))); |
| 104 | return str; |
| 105 | case PTRACE_EVENT_SECCOMP: |
| 106 | snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", |
| 107 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 108 | return str; |
| 109 | case PTRACE_EVENT_STOP: |
| 110 | snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status), |
| 111 | strsignal(WSTOPSIG(status))); |
| 112 | return str; |
| 113 | default: |
| 114 | snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)", |
| 115 | __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 116 | return str; |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 119 | #endif /* defined(PTRACE_EVENT_STOP) */ |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 120 | |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 121 | snprintf( |
| 122 | str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 123 | return str; |
| 124 | } |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 125 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 126 | bool subproc_persistentModeRoundDone(run_t* run) { |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 127 | if (!run->global->exe.persistent) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 128 | return false; |
| 129 | } |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 130 | uint8_t rcv; |
| 131 | if (recv(run->persistentSock, &rcv, sizeof(rcv), MSG_DONTWAIT) != sizeof(rcv)) { |
| 132 | return false; |
| 133 | } |
| 134 | if (rcv == HFdoneTag) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 135 | return true; |
| 136 | } |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 137 | LOG_F("Received invalid message from the persistent process: '%c' (0x%" PRIx8 |
| 138 | ") , expected '%c' (0x%" PRIx8 ")", |
| 139 | rcv, rcv, HFdoneTag, HFdoneTag); |
| 140 | return false; |
| 141 | } |
| 142 | |
Robert Swiecki | 7c81c27 | 2018-01-11 14:41:04 +0100 | [diff] [blame] | 143 | static bool subproc_persistentSendFileIndicator(run_t* run) { |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 144 | uint64_t len = (uint64_t)run->dynamicFileSz; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 145 | if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) { |
Robert Swiecki | 2036e20 | 2017-06-06 00:01:44 +0200 | [diff] [blame] | 146 | PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len)); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 147 | return false; |
| 148 | } |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 152 | static bool subproc_PrepareExecv(run_t* run) { |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 153 | /* |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 154 | * The address space limit. If big enough - roughly the size of RAM used |
| 155 | */ |
Robert Swiecki | 0e6ca61 | 2018-01-16 21:48:30 +0100 | [diff] [blame] | 156 | #ifdef RLIMIT_AS |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 157 | if (run->global->exe.asLimit) { |
Robert Swiecki | dc8047c | 2018-01-07 21:18:26 +0100 | [diff] [blame] | 158 | const struct rlimit rl = { |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 159 | .rlim_cur = run->global->exe.asLimit * 1024ULL * 1024ULL, |
| 160 | .rlim_max = run->global->exe.asLimit * 1024ULL * 1024ULL, |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 161 | }; |
| 162 | if (setrlimit(RLIMIT_AS, &rl) == -1) { |
Robert Swiecki | 8954afd | 2017-11-14 18:14:22 +0100 | [diff] [blame] | 163 | PLOG_W("Couldn't enforce the RLIMIT_AS resource limit, ignoring"); |
| 164 | } |
| 165 | } |
Robert Swiecki | 0e6ca61 | 2018-01-16 21:48:30 +0100 | [diff] [blame] | 166 | #endif /* ifdef RLIMIT_AS */ |
| 167 | #ifdef RLIMIT_RSS |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 168 | if (run->global->exe.rssLimit) { |
Robert Swiecki | dc8047c | 2018-01-07 21:18:26 +0100 | [diff] [blame] | 169 | const struct rlimit rl = { |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 170 | .rlim_cur = run->global->exe.rssLimit * 1024ULL * 1024ULL, |
| 171 | .rlim_max = run->global->exe.rssLimit * 1024ULL * 1024ULL, |
Robert Swiecki | 8954afd | 2017-11-14 18:14:22 +0100 | [diff] [blame] | 172 | }; |
| 173 | if (setrlimit(RLIMIT_RSS, &rl) == -1) { |
| 174 | PLOG_W("Couldn't enforce the RLIMIT_RSS resource limit, ignoring"); |
| 175 | } |
| 176 | } |
Robert Swiecki | 0e6ca61 | 2018-01-16 21:48:30 +0100 | [diff] [blame] | 177 | #endif /* ifdef RLIMIT_RSS */ |
| 178 | #ifdef RLIMIT_DATA |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 179 | if (run->global->exe.dataLimit) { |
Robert Swiecki | dc8047c | 2018-01-07 21:18:26 +0100 | [diff] [blame] | 180 | const struct rlimit rl = { |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 181 | .rlim_cur = run->global->exe.dataLimit * 1024ULL * 1024ULL, |
| 182 | .rlim_max = run->global->exe.dataLimit * 1024ULL * 1024ULL, |
Robert Swiecki | 8954afd | 2017-11-14 18:14:22 +0100 | [diff] [blame] | 183 | }; |
Robert Swiecki | 1f9f213 | 2017-11-27 16:49:37 +0100 | [diff] [blame] | 184 | if (setrlimit(RLIMIT_DATA, &rl) == -1) { |
Robert Swiecki | 8954afd | 2017-11-14 18:14:22 +0100 | [diff] [blame] | 185 | PLOG_W("Couldn't enforce the RLIMIT_DATA resource limit, ignoring"); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 186 | } |
| 187 | } |
Robert Swiecki | 0e6ca61 | 2018-01-16 21:48:30 +0100 | [diff] [blame] | 188 | #endif /* ifdef RLIMIT_DATA */ |
Robert Swiecki | c58ca51 | 2018-11-06 17:50:37 +0100 | [diff] [blame^] | 189 | #ifdef RLIMIT_CORE |
| 190 | if (run->global->exe.dataLimit) { |
| 191 | const struct rlimit rl = { |
| 192 | .rlim_cur = run->global->exe.coreLimit * 1024ULL * 1024ULL, |
| 193 | .rlim_max = run->global->exe.coreLimit * 1024ULL * 1024ULL, |
| 194 | }; |
| 195 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 196 | PLOG_W("Couldn't enforce the RLIMIT_CORE resource limit, ignoring"); |
| 197 | } |
| 198 | } |
| 199 | #endif /* ifdef RLIMIT_CORE */ |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 200 | |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 201 | if (run->global->exe.clearEnv) { |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 202 | environ = NULL; |
| 203 | } |
Robert Swiecki | 97d0cee | 2017-12-18 00:17:50 +0100 | [diff] [blame] | 204 | for (size_t i = 0; i < ARRAYSIZE(run->global->exe.envs) && run->global->exe.envs[i]; i++) { |
| 205 | putenv(run->global->exe.envs[i]); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 206 | } |
| 207 | char fuzzNo[128]; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 208 | snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, run->fuzzNo); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 209 | setenv(_HF_THREAD_NO_ENV, fuzzNo, 1); |
| 210 | |
| 211 | setsid(); |
| 212 | |
Robert Swiecki | 6ab8f34 | 2018-01-15 23:03:16 +0100 | [diff] [blame] | 213 | util_closeStdio(/* close_stdin= */ run->global->exe.nullifyStdio, |
Robert Swiecki | b433306 | 2018-01-15 23:00:45 +0100 | [diff] [blame] | 214 | /* close_stdout= */ run->global->exe.nullifyStdio, |
| 215 | /* close_stderr= */ run->global->exe.nullifyStdio); |
| 216 | |
Robert Swiecki | 6526a7d | 2018-01-15 17:38:58 +0100 | [diff] [blame] | 217 | /* The bitmap structure */ |
Robert Swiecki | a5b918a | 2018-03-07 23:59:53 +0100 | [diff] [blame] | 218 | if (run->global->feedback.bbFd != -1 && dup2(run->global->feedback.bbFd, _HF_BITMAP_FD) == -1) { |
| 219 | PLOG_E("dup2(%d, _HF_BITMAP_FD=%d)", run->global->feedback.bbFd, _HF_BITMAP_FD); |
Robert Swiecki | 6526a7d | 2018-01-15 17:38:58 +0100 | [diff] [blame] | 220 | return false; |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Robert Swiecki | 6526a7d | 2018-01-15 17:38:58 +0100 | [diff] [blame] | 223 | /* The input file to _HF_INPUT_FD */ |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 224 | if (run->global->exe.persistent && dup2(run->dynamicFileFd, _HF_INPUT_FD) == -1) { |
Robert Swiecki | 0a01ea7 | 2018-01-11 01:50:18 +0100 | [diff] [blame] | 225 | PLOG_E("dup2('%d', _HF_INPUT_FD='%d')", run->dynamicFileFd, _HF_INPUT_FD); |
| 226 | return false; |
| 227 | } |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 228 | |
Robert Swiecki | 491d7bb | 2017-02-28 03:02:53 +0100 | [diff] [blame] | 229 | sigset_t sset; |
| 230 | sigemptyset(&sset); |
| 231 | if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) { |
| 232 | PLOG_W("sigprocmask(empty_set)"); |
| 233 | } |
| 234 | |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 235 | if (!run->global->exe.persistent) { |
Robert Swiecki | 1f1a2f9 | 2018-01-15 15:26:37 +0100 | [diff] [blame] | 236 | if ((run->dynamicFileCopyFd = files_writeBufToTmpFile( |
| 237 | run->global->io.workDir, run->dynamicFile, run->dynamicFileSz, 0)) == -1) { |
| 238 | LOG_E("Couldn't save data to a temporary file"); |
| 239 | return false; |
| 240 | } |
| 241 | if (run->global->exe.fuzzStdin && dup2(run->dynamicFileCopyFd, STDIN_FILENO) == -1) { |
| 242 | PLOG_E("dup2(_HF_INPUT_FD=%d, STDIN_FILENO=%d)", run->dynamicFileCopyFd, STDIN_FILENO); |
| 243 | return false; |
| 244 | } |
Robert Swiecki | 3975afe | 2018-01-10 04:32:21 +0100 | [diff] [blame] | 245 | } |
Robert Swiecki | c8a9536 | 2018-01-10 02:29:49 +0100 | [diff] [blame] | 246 | |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 247 | return true; |
| 248 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 249 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 250 | static bool subproc_New(run_t* run) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 251 | run->pid = run->persistentPid; |
dobin | edf9f8d | 2018-01-21 13:57:02 +0100 | [diff] [blame] | 252 | if (run->pid != 0 && run->hasCrashed == false) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 253 | return true; |
| 254 | } |
dobin | edf9f8d | 2018-01-21 13:57:02 +0100 | [diff] [blame] | 255 | |
| 256 | LOG_D("SocketFuzzer: subproc_new: Start New Process"); |
| 257 | run->hasCrashed = false; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 258 | run->tmOutSignaled = false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 259 | |
| 260 | int sv[2]; |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 261 | if (run->global->exe.persistent) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 262 | if (run->persistentSock != -1) { |
| 263 | close(run->persistentSock); |
Jagger | 82a20c0 | 2016-09-02 01:36:39 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Jagger | e4dad65 | 2016-09-10 03:35:50 +0200 | [diff] [blame] | 266 | int sock_type = SOCK_STREAM; |
| 267 | #if defined(SOCK_CLOEXEC) |
| 268 | sock_type |= SOCK_CLOEXEC; |
| 269 | #endif |
| 270 | if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 271 | PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)"); |
| 272 | return false; |
| 273 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 274 | run->persistentSock = sv[0]; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Robert Swiecki | 158fe04 | 2018-01-10 02:33:56 +0100 | [diff] [blame] | 277 | LOG_D("Forking new process for thread: %" PRId32, run->fuzzNo); |
| 278 | |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 279 | run->pid = arch_fork(run); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 280 | if (run->pid == -1) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 281 | PLOG_E("Couldn't fork"); |
| 282 | return false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 283 | } |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 284 | /* The child process */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 285 | if (!run->pid) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 286 | logMutexReset(); |
| 287 | /* |
| 288 | * Reset sighandlers, and set alarm(1). It's a guarantee against dead-locks |
| 289 | * in the child, where we ensure here that the child process will either |
| 290 | * execve or get signaled by SIGALRM within 1 second. |
Robert Swiecki | c337d4f | 2017-06-01 13:09:41 +0200 | [diff] [blame] | 291 | * |
| 292 | * Those deadlocks typically stem from the fact, that malloc() can behave weirdly |
| 293 | * when fork()-ing a single thread of a process: e.g. with glibc < 2.24 |
| 294 | * (or, Ubuntu's 2.23-0ubuntu6). For more see |
| 295 | * 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] | 296 | */ |
| 297 | alarm(1); |
| 298 | signal(SIGALRM, SIG_DFL); |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 299 | |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 300 | if (run->global->exe.persistent) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 301 | if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) { |
| 302 | PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD); |
| 303 | } |
| 304 | close(sv[0]); |
| 305 | close(sv[1]); |
| 306 | } |
| 307 | |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 308 | if (!subproc_PrepareExecv(run)) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 309 | LOG_E("subproc_PrepareExecv() failed"); |
| 310 | exit(EXIT_FAILURE); |
| 311 | } |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 312 | if (!arch_launchChild(run)) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 313 | LOG_E("Error launching child process"); |
Robert Swiecki | 82c707c | 2017-11-14 16:36:23 +0100 | [diff] [blame] | 314 | kill(run->global->threads.mainPid, SIGTERM); |
Robert Swiecki | a375f4b | 2017-06-01 13:20:54 +0200 | [diff] [blame] | 315 | _exit(1); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 316 | } |
Robert Swiecki | 42cc982 | 2017-02-12 22:20:46 +0100 | [diff] [blame] | 317 | abort(); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 318 | } |
Robert Swiecki | c337d4f | 2017-06-01 13:09:41 +0200 | [diff] [blame] | 319 | |
| 320 | /* Parent */ |
Robert Swiecki | 158fe04 | 2018-01-10 02:33:56 +0100 | [diff] [blame] | 321 | LOG_D("Launched new process, PID: %d, thread: %" PRId32 " (concurrency: %zd)", run->pid, |
| 322 | run->fuzzNo, run->global->threads.threadsMax); |
Robert Swiecki | 5eeb29b | 2018-01-21 16:07:06 +0100 | [diff] [blame] | 323 | |
Robert Swiecki | 5e26bd9 | 2018-03-02 12:09:34 +0100 | [diff] [blame] | 324 | if (run->global->socketFuzzer.enabled) { |
Robert Swiecki | 5eeb29b | 2018-01-21 16:07:06 +0100 | [diff] [blame] | 325 | /* (dobin): Don't know why, but this is important */ |
dobin | edf9f8d | 2018-01-21 13:57:02 +0100 | [diff] [blame] | 326 | run->persistentPid = run->pid; |
| 327 | } |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 328 | if (run->global->exe.persistent) { |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 329 | close(sv[1]); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 330 | LOG_I("Persistent mode: Launched new persistent PID: %d", (int)run->pid); |
| 331 | run->persistentPid = run->pid; |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 332 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 333 | |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 334 | arch_prepareParentAfterFork(run); |
Robert Swiecki | 810d2c9 | 2017-06-01 21:37:27 +0200 | [diff] [blame] | 335 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 336 | return true; |
| 337 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 338 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 339 | bool subproc_Run(run_t* run) { |
Robert Swiecki | f2da05a | 2018-01-12 03:01:09 +0100 | [diff] [blame] | 340 | run->timeStartedMillis = util_timeNowMillis(); |
| 341 | |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 342 | if (!subproc_New(run)) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 343 | LOG_E("subproc_New()"); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 344 | return false; |
| 345 | } |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 346 | |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 347 | arch_prepareParent(run); |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 348 | |
Robert Swiecki | 5cc1f7b | 2018-01-16 20:55:43 +0100 | [diff] [blame] | 349 | if (run->global->exe.persistent && !subproc_persistentSendFileIndicator(run)) { |
Robert Swiecki | 599dee1 | 2018-01-10 02:21:58 +0100 | [diff] [blame] | 350 | LOG_W("Could not send file size to the persistent process"); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 351 | kill(run->persistentPid, SIGKILL); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 352 | } |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 353 | arch_reapChild(run); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 354 | |
| 355 | return true; |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 356 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 357 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 358 | uint8_t subproc_System(run_t* run, const char* const argv[]) { |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 359 | pid_t pid = arch_fork(run); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 360 | if (pid == -1) { |
| 361 | PLOG_E("Couldn't fork"); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 362 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 363 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 364 | if (!pid) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 365 | logMutexReset(); |
Robert Swiecki | 5dbab88 | 2017-10-20 16:00:20 +0200 | [diff] [blame] | 366 | |
Robert Swiecki | 3d66b7e | 2018-01-17 00:21:59 +0100 | [diff] [blame] | 367 | setsid(); |
| 368 | util_closeStdio( |
| 369 | /* close_stdin= */ true, /* close_stdout= */ false, /* close_stderr= */ false); |
| 370 | |
Robert Swiecki | 5dbab88 | 2017-10-20 16:00:20 +0200 | [diff] [blame] | 371 | sigset_t sset; |
| 372 | sigemptyset(&sset); |
| 373 | if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) { |
| 374 | PLOG_W("sigprocmask(empty_set)"); |
| 375 | } |
| 376 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 377 | execv(argv[0], (char* const*)&argv[0]); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 378 | PLOG_F("Couldn't execute '%s'", argv[0]); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 379 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | int status; |
| 383 | int flags = 0; |
| 384 | #if defined(__WNOTHREAD) |
| 385 | flags |= __WNOTHREAD; |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 386 | #endif /* defined(__WNOTHREAD) */ |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 387 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 388 | for (;;) { |
| 389 | int ret = wait4(pid, &status, flags, NULL); |
| 390 | if (ret == -1 && errno == EINTR) { |
| 391 | continue; |
| 392 | } |
| 393 | if (ret == -1) { |
| 394 | PLOG_E("wait4() for process PID: %d", (int)pid); |
| 395 | return 255; |
| 396 | } |
| 397 | if (ret != pid) { |
| 398 | LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid); |
| 399 | return 255; |
| 400 | } |
| 401 | if (WIFSIGNALED(status)) { |
| 402 | LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status)); |
| 403 | return (100 + WTERMSIG(status)); |
| 404 | } |
| 405 | if (WIFEXITED(status)) { |
| 406 | if (WEXITSTATUS(status) == 0) { |
| 407 | return 0U; |
| 408 | } |
| 409 | LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status)); |
| 410 | return 1U; |
| 411 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 412 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 413 | LOG_D("wait4() returned with status: %d", status); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 414 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 415 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 416 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 417 | void subproc_checkTimeLimit(run_t* run) { |
Robert Swiecki | 371e129 | 2017-12-18 01:10:33 +0100 | [diff] [blame] | 418 | if (run->global->timing.tmOut == 0) { |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | int64_t curMillis = util_timeNowMillis(); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 423 | int64_t diffMillis = curMillis - run->timeStartedMillis; |
Robert Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 424 | |
Robert Swiecki | 371e129 | 2017-12-18 01:10:33 +0100 | [diff] [blame] | 425 | if (run->tmOutSignaled && (diffMillis > ((run->global->timing.tmOut + 1) * 1000))) { |
Robert Swiecki | 66d3e25 | 2017-07-06 01:53:40 +0200 | [diff] [blame] | 426 | /* Has this instance been already signaled due to timeout? Just, SIGKILL it */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 427 | LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", run->pid); |
| 428 | kill(run->pid, SIGKILL); |
Robert Swiecki | 66d3e25 | 2017-07-06 01:53:40 +0200 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
Robert Swiecki | 371e129 | 2017-12-18 01:10:33 +0100 | [diff] [blame] | 432 | if ((diffMillis > (run->global->timing.tmOut * 1000)) && !run->tmOutSignaled) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 433 | run->tmOutSignaled = true; |
Robert Swiecki | 78633d1 | 2017-11-13 23:24:55 +0100 | [diff] [blame] | 434 | LOG_W("PID %d took too much time (limit %ld s). Killing it with %s", run->pid, |
Robert Swiecki | b8db4b4 | 2018-01-16 22:13:30 +0100 | [diff] [blame] | 435 | (long)run->global->timing.tmOut, |
| 436 | run->global->timing.tmoutVTALRM ? "SIGVTALRM" : "SIGKILL"); |
Robert Swiecki | eba2717 | 2017-12-18 01:12:02 +0100 | [diff] [blame] | 437 | if (run->global->timing.tmoutVTALRM) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 438 | kill(run->pid, SIGVTALRM); |
Robert Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 439 | } else { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 440 | kill(run->pid, SIGKILL); |
Robert Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 441 | } |
Robert Swiecki | 2542dc0 | 2017-11-14 03:35:59 +0100 | [diff] [blame] | 442 | ATOMIC_POST_INC(run->global->cnts.timeoutedCnt); |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 443 | } |
| 444 | } |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 445 | |
Robert Swiecki | d50ed42 | 2017-11-13 23:32:26 +0100 | [diff] [blame] | 446 | void subproc_checkTermination(run_t* run) { |
Robert Swiecki | 0dde76d | 2017-11-16 19:25:44 +0100 | [diff] [blame] | 447 | if (fuzz_isTerminating()) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame] | 448 | LOG_D("Killing PID: %d", (int)run->pid); |
| 449 | kill(run->pid, SIGKILL); |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 450 | } |
| 451 | } |