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 | 81f1662 | 2017-09-28 18:01:45 +0200 | [diff] [blame] | 43 | #include "libcommon/common.h" |
Robert Swiecki | 2d01690 | 2017-05-24 02:12:30 +0200 | [diff] [blame] | 44 | #include "libcommon/files.h" |
| 45 | #include "libcommon/log.h" |
Robert Swiecki | 2d01690 | 2017-05-24 02:12:30 +0200 | [diff] [blame] | 46 | #include "libcommon/util.h" |
Robert Swiecki | ec7b845 | 2017-06-01 13:25:56 +0200 | [diff] [blame] | 47 | #include "sanitizers.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 | 4e595fb | 2017-10-11 17:26:51 +0200 | [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 | { |
| 53 | if (WIFEXITED(status)) { |
| 54 | snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status)); |
| 55 | return str; |
| 56 | } |
| 57 | |
| 58 | if (WIFSIGNALED(status)) { |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 59 | snprintf( |
| 60 | str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status), strsignal(WTERMSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 76 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 85 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 89 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 93 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 97 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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)", |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 101 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 105 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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), |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 109 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 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 | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 113 | strsignal(WSTOPSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 114 | return str; |
| 115 | default: |
| 116 | snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)", |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 117 | __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 118 | return str; |
| 119 | } |
| 120 | } |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 121 | #endif /* defined(PTRACE_EVENT_STOP) */ |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 122 | |
Robert Swiecki | 0b56611 | 2017-10-17 17:39:07 +0200 | [diff] [blame] | 123 | snprintf( |
| 124 | str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 125 | return str; |
| 126 | } |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 127 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 128 | bool subproc_persistentModeRoundDone(honggfuzz_t* hfuzz, run_t* run) |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 129 | { |
Robert Swiecki | 059db07 | 2017-10-20 20:03:49 +0200 | [diff] [blame] | 130 | if (!hfuzz->persistent) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 131 | return false; |
| 132 | } |
Robert Swiecki | bcfdb97 | 2016-11-23 20:33:41 +0100 | [diff] [blame] | 133 | char z; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 134 | if (recv(run->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 135 | LOG_D("Persistent mode round finished"); |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 141 | static bool subproc_persistentSendFile(run_t* run) |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 142 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 143 | uint32_t len = (uint64_t)run->dynamicFileSz; |
| 144 | if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) { |
Robert Swiecki | 2036e20 | 2017-06-06 00:01:44 +0200 | [diff] [blame] | 145 | PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len)); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 146 | return false; |
| 147 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 148 | if (!files_sendToSocketNB(run->persistentSock, run->dynamicFile, run->dynamicFileSz)) { |
| 149 | PLOG_W("files_sendToSocketNB(len=%zu)", run->dynamicFileSz); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 155 | bool subproc_PrepareExecv(honggfuzz_t* hfuzz, run_t* run, const char* fileName) |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 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 | } |
Robert Swiecki | 059db07 | 2017-10-20 20:03:49 +0200 | [diff] [blame] | 186 | if (!sanitizers_prepareExecve(hfuzz)) { |
Anestis Bechtsoudis | e5f09f8 | 2016-12-27 16:06:05 +0200 | [diff] [blame] | 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]; |
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 | |
| 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 | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 215 | static bool subproc_New(honggfuzz_t* hfuzz, run_t* run) |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 216 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 217 | run->pid = run->persistentPid; |
| 218 | if (run->pid != 0) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 219 | return true; |
| 220 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 221 | run->tmOutSignaled = false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 222 | |
| 223 | int sv[2]; |
| 224 | if (hfuzz->persistent) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 225 | if (run->persistentSock != -1) { |
| 226 | close(run->persistentSock); |
Jagger | 82a20c0 | 2016-09-02 01:36:39 +0200 | [diff] [blame] | 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 | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 237 | run->persistentSock = sv[0]; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 240 | run->pid = arch_fork(hfuzz, run); |
| 241 | if (run->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 */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 246 | if (!run->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); |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 260 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 261 | if (hfuzz->persistent) { |
| 262 | if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) { |
| 263 | PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD); |
| 264 | } |
| 265 | close(sv[0]); |
| 266 | close(sv[1]); |
| 267 | } |
| 268 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 269 | if (!subproc_PrepareExecv(hfuzz, run, run->fileName)) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 270 | LOG_E("subproc_PrepareExecv() failed"); |
| 271 | exit(EXIT_FAILURE); |
| 272 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 273 | if (!arch_launchChild(hfuzz, run->fileName)) { |
Robert Swiecki | a7ecf16 | 2017-02-12 23:17:41 +0100 | [diff] [blame] | 274 | kill(hfuzz->mainPid, SIGTERM); |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 275 | LOG_E("Error launching child process"); |
Robert Swiecki | a375f4b | 2017-06-01 13:20:54 +0200 | [diff] [blame] | 276 | _exit(1); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 277 | } |
Robert Swiecki | 42cc982 | 2017-02-12 22:20:46 +0100 | [diff] [blame] | 278 | abort(); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 279 | } |
Robert Swiecki | c337d4f | 2017-06-01 13:09:41 +0200 | [diff] [blame] | 280 | |
| 281 | /* Parent */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 282 | LOG_D("Launched new process, pid: %d, (concurrency: %zd)", run->pid, hfuzz->threadsMax); |
Jagger | 68f3359 | 2016-09-03 04:52:16 +0200 | [diff] [blame] | 283 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 284 | if (hfuzz->persistent) { |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 285 | close(sv[1]); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 286 | LOG_I("Persistent mode: Launched new persistent PID: %d", (int)run->pid); |
| 287 | run->persistentPid = run->pid; |
Robert Swiecki | 9415fe6 | 2017-06-05 23:10:39 +0200 | [diff] [blame] | 288 | |
| 289 | int sndbuf = hfuzz->maxFileSz + 256; |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 290 | if (setsockopt(run->persistentSock, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) == -1) { |
Robert Swiecki | 9415fe6 | 2017-06-05 23:10:39 +0200 | [diff] [blame] | 291 | LOG_W("Couldn't set FD send buffer to '%d' bytes", sndbuf); |
| 292 | } |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 293 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 294 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 295 | arch_prepareParentAfterFork(hfuzz, run); |
Robert Swiecki | 810d2c9 | 2017-06-01 21:37:27 +0200 | [diff] [blame] | 296 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 297 | return true; |
| 298 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 299 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 300 | bool subproc_Run(honggfuzz_t* hfuzz, run_t* run) |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 301 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 302 | if (!subproc_New(hfuzz, run)) { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 303 | LOG_E("subproc_New()"); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 304 | return false; |
| 305 | } |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 306 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 307 | arch_prepareParent(hfuzz, run); |
| 308 | if (hfuzz->persistent && !subproc_persistentSendFile(run)) { |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 309 | LOG_W("Could not send file contents to the persistent process"); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 310 | kill(run->persistentPid, SIGKILL); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 311 | } |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 312 | arch_reapChild(hfuzz, run); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 313 | |
| 314 | return true; |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 315 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 316 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 317 | uint8_t subproc_System(honggfuzz_t* hfuzz, run_t* run, const char* const argv[]) |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 318 | { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 319 | pid_t pid = arch_fork(hfuzz, run); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 320 | if (pid == -1) { |
| 321 | PLOG_E("Couldn't fork"); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 322 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 323 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 324 | if (!pid) { |
Robert Swiecki | 1c9d809 | 2017-06-01 02:39:09 +0200 | [diff] [blame] | 325 | logMutexReset(); |
Robert Swiecki | 5dbab88 | 2017-10-20 16:00:20 +0200 | [diff] [blame] | 326 | |
| 327 | sigset_t sset; |
| 328 | sigemptyset(&sset); |
| 329 | if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) { |
| 330 | PLOG_W("sigprocmask(empty_set)"); |
| 331 | } |
| 332 | |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 333 | execv(argv[0], (char* const*)&argv[0]); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 334 | PLOG_F("Couldn't execute '%s'", argv[0]); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 335 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int status; |
| 339 | int flags = 0; |
| 340 | #if defined(__WNOTHREAD) |
| 341 | flags |= __WNOTHREAD; |
Robert Swiecki | 4e595fb | 2017-10-11 17:26:51 +0200 | [diff] [blame] | 342 | #endif /* defined(__WNOTHREAD) */ |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 343 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 344 | for (;;) { |
| 345 | int ret = wait4(pid, &status, flags, NULL); |
| 346 | if (ret == -1 && errno == EINTR) { |
| 347 | continue; |
| 348 | } |
| 349 | if (ret == -1) { |
| 350 | PLOG_E("wait4() for process PID: %d", (int)pid); |
| 351 | return 255; |
| 352 | } |
| 353 | if (ret != pid) { |
| 354 | LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid); |
| 355 | return 255; |
| 356 | } |
| 357 | if (WIFSIGNALED(status)) { |
| 358 | LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status)); |
| 359 | return (100 + WTERMSIG(status)); |
| 360 | } |
| 361 | if (WIFEXITED(status)) { |
| 362 | if (WEXITSTATUS(status) == 0) { |
| 363 | return 0U; |
| 364 | } |
| 365 | LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status)); |
| 366 | return 1U; |
| 367 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 368 | |
Robert Swiecki | 1ddf674 | 2017-05-08 21:17:32 +0200 | [diff] [blame] | 369 | LOG_D("wait4() returned with status: %d", status); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 370 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 371 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 372 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 373 | void subproc_checkTimeLimit(honggfuzz_t* hfuzz, run_t* run) |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 374 | { |
| 375 | if (hfuzz->tmOut == 0) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | int64_t curMillis = util_timeNowMillis(); |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 380 | int64_t diffMillis = curMillis - run->timeStartedMillis; |
Robert Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 381 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 382 | if (run->tmOutSignaled && (diffMillis > ((hfuzz->tmOut + 1) * 1000))) { |
Robert Swiecki | 66d3e25 | 2017-07-06 01:53:40 +0200 | [diff] [blame] | 383 | /* Has this instance been already signaled due to timeout? Just, SIGKILL it */ |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 384 | LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", run->pid); |
| 385 | kill(run->pid, SIGKILL); |
Robert Swiecki | 66d3e25 | 2017-07-06 01:53:40 +0200 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 389 | if ((diffMillis > (hfuzz->tmOut * 1000)) && !run->tmOutSignaled) { |
| 390 | run->tmOutSignaled = true; |
| 391 | LOG_W("PID %d took too much time (limit %ld s). Killing it with %s", run->pid, hfuzz->tmOut, |
| 392 | hfuzz->tmout_vtalrm ? "SIGVTALRM" : "SIGKILL"); |
Robert Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 393 | if (hfuzz->tmout_vtalrm) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 394 | kill(run->pid, SIGVTALRM); |
Robert Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 395 | } else { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 396 | kill(run->pid, SIGKILL); |
Robert Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 397 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 398 | ATOMIC_POST_INC(hfuzz->timeoutedCnt); |
| 399 | } |
| 400 | } |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 401 | |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 402 | void subproc_checkTermination(honggfuzz_t* hfuzz, run_t* run) |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 403 | { |
| 404 | if (ATOMIC_GET(hfuzz->terminating)) { |
Robert Swiecki | e7294ca | 2017-11-11 02:46:32 +0100 | [diff] [blame^] | 405 | LOG_D("Killing PID: %d", (int)run->pid); |
| 406 | kill(run->pid, SIGKILL); |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 407 | } |
| 408 | } |