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