robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 2 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 3 | * honggfuzz - architecture dependent code (LINUX) |
| 4 | * ----------------------------------------- |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 5 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 6 | * Author: Robert Swiecki <swiecki@google.com> |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 7 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 8 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. You may obtain |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 12 | * a copy of the License at |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 13 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 15 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 19 | * implied. See the License for the specific language governing |
| 20 | * permissions and limitations under the License. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 21 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 22 | */ |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 23 | |
| 24 | #include "common.h" |
| 25 | #include "arch.h" |
| 26 | |
| 27 | #include <ctype.h> |
| 28 | #include <errno.h> |
| 29 | #include <signal.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <inttypes.h> |
| 34 | #include <sys/cdefs.h> |
| 35 | #include <sys/personality.h> |
| 36 | #include <sys/ptrace.h> |
| 37 | #include <sys/prctl.h> |
| 38 | #include <sys/resource.h> |
| 39 | #include <sys/stat.h> |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 40 | #include <sys/syscall.h> |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 41 | #include <sys/time.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/user.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <time.h> |
| 46 | #include <unistd.h> |
Anestis Bechtsoudis | be7fa27 | 2015-09-10 17:27:44 +0300 | [diff] [blame^] | 47 | #include <sys/utsname.h> |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 48 | |
robert.swiecki@gmail.com | 6310b11 | 2015-02-17 23:30:45 +0000 | [diff] [blame] | 49 | #include "linux/perf.h" |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 50 | #include "linux/ptrace_utils.h" |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 51 | #include "log.h" |
| 52 | #include "util.h" |
| 53 | |
| 54 | bool arch_launchChild(honggfuzz_t * hfuzz, char *fileName) |
| 55 | { |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 56 | /* |
| 57 | * Kill a process which corrupts its own heap (with ABRT) |
| 58 | */ |
| 59 | if (setenv("MALLOC_CHECK_", "3", 1) == -1) { |
| 60 | LOGMSG_P(l_ERROR, "setenv(MALLOC_CHECK_=3) failed"); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Tell asan to ignore SEGVs |
| 66 | */ |
robert.swiecki@gmail.com | 0377009 | 2015-02-20 13:48:31 +0000 | [diff] [blame] | 67 | if (setenv |
robert.swiecki@gmail.com | 62e34ae | 2015-03-05 03:39:32 +0000 | [diff] [blame] | 68 | ("ASAN_OPTIONS", |
| 69 | "allow_user_segv_handler=1:handle_segv=0:abort_on_error=1:allocator_may_return_null=1", |
| 70 | 1) == -1) { |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 71 | LOGMSG_P(l_ERROR, "setenv(ASAN_OPTIONS) failed"); |
| 72 | return false; |
| 73 | } |
| 74 | |
Jagger | 794b758 | 2015-09-08 01:26:58 +0200 | [diff] [blame] | 75 | const char *msan_options = |
| 76 | "exit_code=" HF_MSAN_EXIT_CODE_STR ":report_umrs=0:wrap_signals=0:print_stats=1"; |
| 77 | if (hfuzz->msanReportUMRS == true) { |
| 78 | msan_options = |
| 79 | "exit_code=" HF_MSAN_EXIT_CODE_STR ":report_umrs=1:wrap_signals=0:print_stats=1"; |
| 80 | } |
| 81 | if (setenv("MSAN_OPTIONS", msan_options, 1) == -1) { |
| 82 | LOGMSG_P(l_ERROR, "setenv(MSAN_OPTIONS) failed"); |
| 83 | return false; |
| 84 | } |
| 85 | |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 86 | /* |
| 87 | * Kill the children when fuzzer dies (e.g. due to Ctrl+C) |
| 88 | */ |
| 89 | if (prctl(PR_SET_PDEATHSIG, (long)SIGKILL, 0L, 0L, 0L) == -1) { |
| 90 | LOGMSG_P(l_ERROR, "prctl(PR_SET_PDEATHSIG, SIGKILL) failed"); |
| 91 | return false; |
| 92 | } |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 93 | /* |
| 94 | * Disable ASLR |
| 95 | */ |
Jagger | 794b758 | 2015-09-08 01:26:58 +0200 | [diff] [blame] | 96 | if (hfuzz->disableRandomization && personality(ADDR_NO_RANDOMIZE) == -1) { |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 97 | LOGMSG_P(l_ERROR, "personality(ADDR_NO_RANDOMIZE) failed"); |
| 98 | return false; |
| 99 | } |
| 100 | #define ARGS_MAX 512 |
| 101 | char *args[ARGS_MAX + 2]; |
Anestis Bechtsoudis | c1f6faa | 2015-07-31 05:32:19 +0300 | [diff] [blame] | 102 | char argData[PATH_MAX] = { 0 }; |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 103 | int x; |
| 104 | |
| 105 | for (x = 0; x < ARGS_MAX && hfuzz->cmdline[x]; x++) { |
| 106 | if (!hfuzz->fuzzStdin && strcmp(hfuzz->cmdline[x], _HF_FILE_PLACEHOLDER) == 0) { |
| 107 | args[x] = fileName; |
Anestis Bechtsoudis | c1f6faa | 2015-07-31 05:32:19 +0300 | [diff] [blame] | 108 | } else if (!hfuzz->fuzzStdin && strstr(hfuzz->cmdline[x], _HF_FILE_PLACEHOLDER)) { |
| 109 | const char *off = strstr(hfuzz->cmdline[x], _HF_FILE_PLACEHOLDER); |
Jagger | 400fd8f | 2015-08-16 10:50:55 +0200 | [diff] [blame] | 110 | snprintf(argData, PATH_MAX, "%.*s%s", (int)(off - hfuzz->cmdline[x]), hfuzz->cmdline[x], |
| 111 | fileName); |
Anestis Bechtsoudis | c1f6faa | 2015-07-31 05:32:19 +0300 | [diff] [blame] | 112 | args[x] = argData; |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 113 | } else { |
| 114 | args[x] = hfuzz->cmdline[x]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | args[x++] = NULL; |
| 119 | |
| 120 | LOGMSG(l_DEBUG, "Launching '%s' on file '%s'", args[0], fileName); |
| 121 | |
| 122 | /* |
| 123 | * Set timeout (prof), real timeout (2*prof), and rlimit_cpu (2*prof) |
| 124 | */ |
| 125 | if (hfuzz->tmOut) { |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 126 | /* |
| 127 | * Set the CPU rlimit to twice the value of the time-out |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 128 | */ |
| 129 | struct rlimit rl = { |
| 130 | .rlim_cur = hfuzz->tmOut * 2, |
| 131 | .rlim_max = hfuzz->tmOut * 2, |
| 132 | }; |
| 133 | if (setrlimit(RLIMIT_CPU, &rl) == -1) { |
| 134 | LOGMSG_P(l_ERROR, "Couldn't enforce the RLIMIT_CPU resource limit"); |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * The address space limit. If big enough - roughly the size of RAM used |
| 141 | */ |
| 142 | if (hfuzz->asLimit) { |
Robert Swiecki | c578d64 | 2015-09-08 16:13:36 +0200 | [diff] [blame] | 143 | struct rlimit64 rl = { |
| 144 | .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL, |
| 145 | .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL, |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 146 | }; |
Robert Swiecki | c578d64 | 2015-09-08 16:13:36 +0200 | [diff] [blame] | 147 | if (prlimit64(getpid(), RLIMIT_AS, &rl, NULL) == -1) { |
Anestis Bechtsoudis | 51c9bb1 | 2015-08-26 14:46:21 +0300 | [diff] [blame] | 148 | LOGMSG_P(l_DEBUG, "Couldn't enforce the RLIMIT_AS resource limit, ignoring"); |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
robert.swiecki | 3da0ea5 | 2015-03-12 14:19:45 +0000 | [diff] [blame] | 152 | for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) { |
| 153 | putenv(hfuzz->envs[i]); |
| 154 | } |
| 155 | |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 156 | if (hfuzz->nullifyStdio) { |
| 157 | util_nullifyStdio(); |
| 158 | } |
| 159 | |
| 160 | if (hfuzz->fuzzStdin) { |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 161 | /* |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 162 | * Uglyyyyyy ;) |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 163 | */ |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 164 | if (!util_redirectStdin(fileName)) { |
| 165 | return false; |
| 166 | } |
| 167 | } |
robert.swiecki | 3da0ea5 | 2015-03-12 14:19:45 +0000 | [diff] [blame] | 168 | /* |
| 169 | * Wait for the ptrace to attach |
| 170 | */ |
robert.swiecki@gmail.com | 59ca7eb | 2015-04-02 02:25:22 +0000 | [diff] [blame] | 171 | syscall(__NR_tkill, syscall(__NR_gettid), SIGSTOP); |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 172 | execvp(args[0], args); |
| 173 | |
| 174 | util_recoverStdio(); |
| 175 | LOGMSG(l_FATAL, "Failed to create new '%s' process", args[0]); |
| 176 | return false; |
| 177 | } |
| 178 | |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 179 | static void arch_sigFunc(int signo, siginfo_t * si, void *dummy) |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 180 | { |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 181 | if (signo != SIGALRM) { |
| 182 | LOGMSG(l_ERROR, "Signal != SIGALRM (%d)", signo); |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 183 | } |
| 184 | return; |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 185 | if (si == NULL) { |
| 186 | return; |
| 187 | } |
| 188 | if (dummy == NULL) { |
| 189 | return; |
| 190 | } |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 191 | } |
| 192 | |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 193 | static void arch_removeTimer(timer_t * timerid) |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 194 | { |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 195 | timer_delete(*timerid); |
| 196 | } |
| 197 | |
| 198 | static bool arch_setTimer(timer_t * timerid) |
| 199 | { |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 200 | struct sigevent sevp = { |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 201 | .sigev_value.sival_ptr = timerid, |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 202 | .sigev_signo = SIGALRM, |
| 203 | .sigev_notify = SIGEV_THREAD_ID | SIGEV_SIGNAL, |
| 204 | ._sigev_un._tid = syscall(__NR_gettid), |
| 205 | }; |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 206 | if (timer_create(CLOCK_REALTIME, &sevp, timerid) == -1) { |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 207 | LOGMSG_P(l_ERROR, "timer_create(CLOCK_REALTIME) failed"); |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 208 | return false; |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 209 | } |
| 210 | /* |
| 211 | * Kick in every 200ms, starting with the next second |
| 212 | */ |
| 213 | const struct itimerspec ts = { |
| 214 | .it_value = {.tv_sec = 1,.tv_nsec = 0}, |
| 215 | .it_interval = {.tv_sec = 0,.tv_nsec = 200000000,}, |
| 216 | }; |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 217 | if (timer_settime(*timerid, 0, &ts, NULL) == -1) { |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 218 | LOGMSG_P(l_ERROR, "timer_settime() failed"); |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 219 | timer_delete(*timerid); |
| 220 | return false; |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 221 | } |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 222 | sigset_t smask; |
| 223 | sigemptyset(&smask); |
| 224 | struct sigaction sa = { |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 225 | .sa_handler = NULL, |
| 226 | .sa_sigaction = arch_sigFunc, |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 227 | .sa_mask = smask, |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 228 | .sa_flags = SA_SIGINFO, |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 229 | .sa_restorer = NULL, |
| 230 | }; |
| 231 | if (sigaction(SIGALRM, &sa, NULL) == -1) { |
Anestis Bechtsoudis | 51c9bb1 | 2015-08-26 14:46:21 +0300 | [diff] [blame] | 232 | LOGMSG_P(l_ERROR, "sigaction(SIGALRM) failed"); |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 233 | return false; |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 234 | } |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 235 | |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 236 | return true; |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void arch_checkTimeLimit(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 240 | { |
robert.swiecki@gmail.com | 3213a11 | 2015-03-12 01:42:02 +0000 | [diff] [blame] | 241 | int64_t curMillis = util_timeNowMillis(); |
| 242 | int64_t diffMillis = curMillis - fuzzer->timeStartedMillis; |
| 243 | if (diffMillis > (hfuzz->tmOut * 1000)) { |
| 244 | LOGMSG(l_WARN, "PID %d took too much time (limit %ld s). Sending SIGKILL", |
| 245 | fuzzer->pid, hfuzz->tmOut); |
robert.swiecki@gmail.com | 1111d13 | 2015-03-12 01:32:26 +0000 | [diff] [blame] | 246 | kill(fuzzer->pid, SIGKILL); |
Jagger | 7acbf2e | 2015-09-06 20:02:32 +0200 | [diff] [blame] | 247 | __sync_fetch_and_add(&hfuzz->timeoutedCnt, 1UL); |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 248 | } |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 249 | } |
| 250 | |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 251 | void arch_reapChild(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 252 | { |
robert.swiecki@gmail.com | 57573b8 | 2015-04-02 00:26:35 +0000 | [diff] [blame] | 253 | pid_t ptracePid = (hfuzz->pid > 0) ? hfuzz->pid : fuzzer->pid; |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 254 | pid_t childPid = fuzzer->pid; |
| 255 | |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 256 | timer_t timerid; |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 257 | if (arch_setTimer(&timerid) == false) { |
robert.swiecki@gmail.com | eb90430 | 2015-03-12 04:50:58 +0000 | [diff] [blame] | 258 | LOGMSG(l_FATAL, "Couldn't set timer"); |
| 259 | } |
robert.swiecki@gmail.com | dbcc7a3 | 2015-03-12 00:22:20 +0000 | [diff] [blame] | 260 | |
Jagger | ade3d74 | 2015-09-10 03:35:15 +0200 | [diff] [blame] | 261 | perfFd_t perfFds; |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 262 | |
Jagger | dfe815f | 2015-06-29 01:59:06 +0200 | [diff] [blame] | 263 | for (;;) { |
| 264 | int status; |
Anestis Bechtsoudis | b419c6e | 2015-09-08 23:45:29 +0300 | [diff] [blame] | 265 | pid_t pid = wait4(childPid, &status, __WNOTHREAD | __WALL | WUNTRACED, NULL); |
Jagger | dfe815f | 2015-06-29 01:59:06 +0200 | [diff] [blame] | 266 | if (pid == -1 && errno == EINTR) { |
| 267 | continue; |
| 268 | } |
| 269 | if (pid != childPid) { |
| 270 | LOGMSG_P(l_FATAL, "wait4()=%d =! %d", pid, childPid); |
| 271 | } |
Jagger | c85fd6e | 2015-06-29 02:02:04 +0200 | [diff] [blame] | 272 | if (WIFSTOPPED(status)) { |
| 273 | break; |
Jagger | dfe815f | 2015-06-29 01:59:06 +0200 | [diff] [blame] | 274 | } |
Jagger | c85fd6e | 2015-06-29 02:02:04 +0200 | [diff] [blame] | 275 | LOGMSG_P(l_FATAL, "PID '%d' is not in a stopped state", pid); |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 276 | } |
Jagger | ade3d74 | 2015-09-10 03:35:15 +0200 | [diff] [blame] | 277 | if (arch_perfEnable(ptracePid, hfuzz, &perfFds) == false) { |
robert.swiecki@gmail.com | 72e5c43 | 2015-04-02 01:27:31 +0000 | [diff] [blame] | 278 | LOGMSG(l_FATAL, "Couldn't enable perf counters for pid %d", ptracePid); |
robert.swiecki@gmail.com | 627c193 | 2015-02-25 02:35:00 +0000 | [diff] [blame] | 279 | } |
Jagger | 8559e55 | 2015-09-05 17:28:44 +0200 | [diff] [blame] | 280 | if (arch_ptraceAttach(ptracePid) == false) { |
| 281 | LOGMSG(l_FATAL, "Couldn't attach to pid %d", ptracePid); |
| 282 | } |
robert.swiecki@gmail.com | b52d92b | 2015-04-02 02:28:06 +0000 | [diff] [blame] | 283 | kill(childPid, SIGCONT); |
robert.swiecki@gmail.com | d781897 | 2015-02-24 23:37:59 +0000 | [diff] [blame] | 284 | |
| 285 | for (;;) { |
robert.swiecki@gmail.com | d526d31 | 2015-03-12 02:12:50 +0000 | [diff] [blame] | 286 | int status; |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 287 | |
Jagger | d628a70 | 2015-08-23 12:59:37 +0200 | [diff] [blame] | 288 | // wait3 syscall is no longer present in Android |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 289 | #if !defined(__ANDROID__) |
Anestis Bechtsoudis | b419c6e | 2015-09-08 23:45:29 +0300 | [diff] [blame] | 290 | pid_t pid = wait3(&status, __WNOTHREAD | __WALL, NULL); |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 291 | #else |
Anestis Bechtsoudis | b419c6e | 2015-09-08 23:45:29 +0300 | [diff] [blame] | 292 | pid_t pid = wait4(fuzzer->pid, &status, __WNOTHREAD | __WALL, NULL); |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 293 | #endif |
robert.swiecki@gmail.com | 6e3d04c | 2015-02-25 02:04:15 +0000 | [diff] [blame] | 294 | |
Jagger | 794b758 | 2015-09-08 01:26:58 +0200 | [diff] [blame] | 295 | LOGMSG(l_DEBUG, "PID '%d' returned with status '%d'", pid, status); |
robert.swiecki@gmail.com | d781897 | 2015-02-24 23:37:59 +0000 | [diff] [blame] | 296 | |
Anestis Bechtsoudis | 5950996 | 2015-09-10 11:40:22 +0300 | [diff] [blame] | 297 | if (pid == -1 && errno == EINTR) { |
| 298 | if (hfuzz->tmOut) { |
| 299 | arch_checkTimeLimit(hfuzz, fuzzer); |
| 300 | } |
robert.swiecki@gmail.com | 004ddfe | 2015-02-25 02:57:39 +0000 | [diff] [blame] | 301 | continue; |
| 302 | } |
| 303 | if (pid == -1 && errno == ECHILD) { |
robert.swiecki@gmail.com | 004ddfe | 2015-02-25 02:57:39 +0000 | [diff] [blame] | 304 | LOGMSG(l_DEBUG, "No more processes to track"); |
robert.swiecki@gmail.com | 8a50da4 | 2015-03-28 00:56:56 +0000 | [diff] [blame] | 305 | break; |
robert.swiecki@gmail.com | 004ddfe | 2015-02-25 02:57:39 +0000 | [diff] [blame] | 306 | } |
| 307 | if (pid == -1) { |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 308 | #if !defined(__ANDROID__) |
Jagger | d628a70 | 2015-08-23 12:59:37 +0200 | [diff] [blame] | 309 | LOGMSG_P(l_FATAL, "wait3() failed"); |
Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame] | 310 | #else |
| 311 | LOGMSG_P(l_FATAL, "wait4() failed"); |
| 312 | #endif |
robert.swiecki@gmail.com | 004ddfe | 2015-02-25 02:57:39 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Jagger | 945a0a4 | 2015-09-09 02:21:10 +0200 | [diff] [blame] | 315 | arch_ptraceGetCustomPerf(hfuzz, ptracePid, &fuzzer->hwCnts.customCnt); |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 316 | |
| 317 | if (ptracePid == childPid) { |
| 318 | arch_ptraceAnalyze(hfuzz, status, pid, fuzzer); |
| 319 | continue; |
| 320 | } |
robert.swiecki@gmail.com | 57573b8 | 2015-04-02 00:26:35 +0000 | [diff] [blame] | 321 | if (pid == childPid && (WIFEXITED(status) || WIFSIGNALED(status))) { |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | if (pid == childPid) { |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | arch_ptraceAnalyze(hfuzz, status, pid, fuzzer); |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 329 | } |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 330 | arch_removeTimer(&timerid); |
Jagger | ade3d74 | 2015-09-10 03:35:15 +0200 | [diff] [blame] | 331 | arch_perfAnalyze(hfuzz, fuzzer, &perfFds); |
robert.swiecki@gmail.com | 8a50da4 | 2015-03-28 00:56:56 +0000 | [diff] [blame] | 332 | return; |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 333 | } |
| 334 | |
robert.swiecki@gmail.com | 6d6f756 | 2015-02-17 22:18:51 +0000 | [diff] [blame] | 335 | bool arch_archInit(honggfuzz_t * hfuzz) |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 336 | { |
Anestis Bechtsoudis | be7fa27 | 2015-09-10 17:27:44 +0300 | [diff] [blame^] | 337 | unsigned long major = 0, minor = 0; |
| 338 | char *p = NULL; |
| 339 | |
| 340 | if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE) { |
| 341 | /* |
| 342 | * Check that linux kernel is compatible |
| 343 | * |
| 344 | * Compatibility list: |
| 345 | * 1) Perf exclude_callchain_kernel requires kernel >= 3.7 |
| 346 | * TODO: Runtime logic to disable it for unsupported kernels |
| 347 | * if it doesn't affect perf counters processing |
| 348 | */ |
| 349 | struct utsname uts; |
| 350 | if (uname(&uts) == -1) { |
| 351 | LOGMSG_P(l_FATAL, "uname() failed"); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | p = uts.release; |
| 356 | major = strtoul(p, &p, 10); |
| 357 | if (*p++ != '.') { |
| 358 | LOGMSG(l_FATAL, "Unsupported kernel version (%s)", uts.release); |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | minor = strtoul(p, &p, 10); |
| 363 | if ((major < 3) || ((major == 3) && (minor < 7))) { |
| 364 | LOGMSG(l_ERROR, "Unsupported kernel version (%s)", uts.release); |
| 365 | return false; |
| 366 | } |
robert.swiecki@gmail.com | e468320 | 2015-04-02 00:10:52 +0000 | [diff] [blame] | 367 | } |
Anestis Bechtsoudis | be7fa27 | 2015-09-10 17:27:44 +0300 | [diff] [blame^] | 368 | #if defined(__ANDROID__) && defined(__arm__) |
| 369 | /* |
| 370 | * For ARM kernels running Android API <= 21, if fuzzing target links to |
| 371 | * libcrypto (OpenSSL), OPENSSL_cpuid_setup initialization is triggering a |
| 372 | * SIGILL/ILLOPC at armv7_tick() due to "mrrc p15, #1, r0, r1, c14)" instruction. |
| 373 | * Setups using BoringSSL (API >= 22) are not affected. |
| 374 | */ |
| 375 | if (setenv("OPENSSL_armcap", OPENSSL_ARMCAP_ABI, 1) == -1) { |
| 376 | LOGMSG_P(l_ERROR, "setenv(OPENSSL_armcap) failed"); |
| 377 | return false; |
| 378 | } |
| 379 | #endif |
| 380 | |
| 381 | return true; |
robert.swiecki@gmail.com | a0d8714 | 2015-02-14 13:11:18 +0000 | [diff] [blame] | 382 | } |