blob: c5dbda4ac43a6dd3425028fa955b1c64f793d6c8 [file] [log] [blame]
robert.swiecki3bb518c2010-10-14 00:48:24 +00001/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00002 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00003 * honggfuzz - architecture dependent code (MAC OS X)
4 * -----------------------------------------
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00005 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00006 * Author: Robert Swiecki <swiecki@google.com> Felix Gröbert
7 * <groebert@google.com>
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00008 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00009 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000010 *
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
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000013 * a copy of the License at
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000014 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000015 * http://www.apache.org/licenses/LICENSE-2.0
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000016 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000017 * 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.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000022 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000023 */
robert.swiecki3bb518c2010-10-14 00:48:24 +000024
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000025#include "common.h"
26#include "arch.h"
27
robert.swiecki3bb518c2010-10-14 00:48:24 +000028#include <ctype.h>
groebert@google.comb857deb2010-10-21 19:09:29 +000029#include <dirent.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000030#include <errno.h>
groebert@google.comb857deb2010-10-21 19:09:29 +000031#include <fcntl.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000032#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/cdefs.h>
37#include <sys/mman.h>
38#include <sys/time.h>
39#include <sys/types.h>
40#include <sys/wait.h>
41#include <sys/resource.h>
42#include <time.h>
43#include <unistd.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000044
robert.swiecki3bb518c2010-10-14 00:48:24 +000045#include "log.h"
robert.swiecki3bb518c2010-10-14 00:48:24 +000046#include "util.h"
groebert@google.comb857deb2010-10-21 19:09:29 +000047#include "files.h"
robert.swiecki3bb518c2010-10-14 00:48:24 +000048
groebert@google.com1bd4c212013-06-19 11:13:56 +000049#include <servers/bootstrap.h>
50#include <mach/mach.h>
51#include <mach/mach_vm.h>
52#include <mach/mach_types.h>
53#include <mach/i386/thread_status.h>
54#include <mach/task_info.h>
55#include <pthread.h>
56
57#include "mach_exc.h"
58#include "mach_excServer.h"
59
60#import <Foundation/Foundation.h>
61
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000062/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000063 * Interface to third_party/CrashReport_*.o
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000064 */
groebert@google.com43216c82015-02-13 11:11:38 +000065/* *INDENT-OFF* */
66@interface CrashReport: NSObject - (id) initWithTask:(task_t)
groebert@google.com1bd4c212013-06-19 11:13:56 +000067task exceptionType:(exception_type_t)
68anExceptionType exceptionCode:(mach_exception_data_t)
69anExceptionCode exceptionCodeCount:(mach_msg_type_number_t)
70anExceptionCodeCount thread:(thread_t)
71thread threadStateFlavor:(thread_state_flavor_t)
72aThreadStateFlavor threadState:(thread_state_data_t)
73aThreadState threadStateCount:(mach_msg_type_number_t) aThreadStateCount;
74@end
groebert@google.com43216c82015-02-13 11:11:38 +000075/* *INDENT-ON* */
76
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000077/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000078 * Global to have exception port available in the collection thread
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000079 */
groebert@google.com1bd4c212013-06-19 11:13:56 +000080static mach_port_t g_exception_port = MACH_PORT_NULL;
81
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000082/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000083 * From xnu/bsd/sys/proc_internal.h
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000084 */
groebert@google.comc659c0e2015-02-13 11:17:00 +000085#define PID_MAX 99999
groebert@google.com43216c82015-02-13 11:11:38 +000086
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000087/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000088 * Global to store crash info in exception handler thread
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000089 */
groebert@google.comc659c0e2015-02-13 11:17:00 +000090fuzzer_t g_fuzzer_crash_information[PID_MAX + 1];
groebert@google.com43216c82015-02-13 11:11:38 +000091
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000092/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000093 * Global to have a unique service name for each honggfuzz process
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000094 */
groebert@google.com76afff92013-06-20 14:43:41 +000095char g_service_name[256];
groebert@google.com16610ee2013-06-19 13:06:12 +000096
robert.swiecki3bb518c2010-10-14 00:48:24 +000097struct {
98 bool important;
99 const char *descr;
100} arch_sigs[NSIG];
101
robert.swiecki@gmail.comc6d28752014-01-08 12:02:37 +0000102__attribute__ ((constructor))
103void arch_initSigs(void)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000104{
105 for (int x = 0; x < NSIG; x++)
106 arch_sigs[x].important = false;
107
108 arch_sigs[SIGILL].important = true;
109 arch_sigs[SIGILL].descr = "SIGILL";
110 arch_sigs[SIGFPE].important = true;
111 arch_sigs[SIGFPE].descr = "SIGFPE";
112 arch_sigs[SIGSEGV].important = true;
113 arch_sigs[SIGSEGV].descr = "SIGSEGV";
114 arch_sigs[SIGBUS].important = true;
115 arch_sigs[SIGBUS].descr = "SIGBUS";
116 arch_sigs[SIGABRT].important = true;
117 arch_sigs[SIGABRT].descr = "SIGABRT";
118}
119
groebert@google.com1bd4c212013-06-19 11:13:56 +0000120const char *exception_to_string(int exception)
groebert@google.comb857deb2010-10-21 19:09:29 +0000121{
groebert@google.com1bd4c212013-06-19 11:13:56 +0000122 switch (exception) {
123 case EXC_BAD_ACCESS:
124 return "EXC_BAD_ACCESS";
125 case EXC_BAD_INSTRUCTION:
126 return "EXC_BAD_INSTRUCTION";
127 case EXC_ARITHMETIC:
128 return "EXC_ARITHMETIC";
129 case EXC_EMULATION:
130 return "EXC_EMULATION";
131 case EXC_SOFTWARE:
132 return "EXC_SOFTWARE";
133 case EXC_BREAKPOINT:
134 return "EXC_BREAKPOINT";
135 case EXC_SYSCALL:
136 return "EXC_SYSCALL";
137 case EXC_MACH_SYSCALL:
138 return "EXC_MACH_SYSCALL";
139 case EXC_RPC_ALERT:
140 return "EXC_RPC_ALERT";
141 case EXC_CRASH:
142 return "EXC_CRASH";
groebert@google.comb857deb2010-10-21 19:09:29 +0000143 }
groebert@google.com1bd4c212013-06-19 11:13:56 +0000144 return "UNKNOWN";
groebert@google.comb857deb2010-10-21 19:09:29 +0000145}
146
147/*
robert.swiecki3bb518c2010-10-14 00:48:24 +0000148 * Returns true if a process exited (so, presumably, we can delete an input
149 * file)
150 */
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000151static bool arch_analyzeSignal(honggfuzz_t * hfuzz, int status, fuzzer_t * fuzzer)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000152{
153 /*
154 * Resumed by delivery of SIGCONT
155 */
156 if (WIFCONTINUED(status)) {
157 return false;
158 }
159
160 /*
161 * Boring, the process just exited
162 */
163 if (WIFEXITED(status)) {
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000164 LOGMSG(l_DEBUG, "Process (pid %d) exited normally with status %d", fuzzer->pid,
165 WEXITSTATUS(status));
robert.swiecki3bb518c2010-10-14 00:48:24 +0000166 return true;
167 }
168
169 /*
170 * Shouldn't really happen, but, well..
171 */
172 if (!WIFSIGNALED(status)) {
173 LOGMSG(l_ERROR,
174 "Process (pid %d) exited with the following status %d, please report that as a bug",
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000175 fuzzer->pid, status);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000176 return true;
177 }
178
179 int termsig = WTERMSIG(status);
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000180 LOGMSG(l_DEBUG, "Process (pid %d) killed by signal %d '%s'", fuzzer->pid, termsig,
181 strsignal(termsig));
robert.swiecki3bb518c2010-10-14 00:48:24 +0000182 if (!arch_sigs[termsig].important) {
183 LOGMSG(l_DEBUG, "It's not that important signal, skipping");
184 return true;
185 }
186
groebert@google.com1bd4c212013-06-19 11:13:56 +0000187 /*
188 * Signal is interesting
189 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000190 char newname[PATH_MAX];
groebert@google.comb857deb2010-10-21 19:09:29 +0000191
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000192 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000193 * Get data from exception handler
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000194 */
groebert@google.com43216c82015-02-13 11:11:38 +0000195 fuzzer->pc = g_fuzzer_crash_information[fuzzer->pid].pc;
196 fuzzer->exception = g_fuzzer_crash_information[fuzzer->pid].exception;
197 fuzzer->access = g_fuzzer_crash_information[fuzzer->pid].access;
198 fuzzer->backtrace = g_fuzzer_crash_information[fuzzer->pid].backtrace;
199
groebert@google.com1bd4c212013-06-19 11:13:56 +0000200 if (hfuzz->saveUnique) {
201 snprintf(newname, sizeof(newname),
202 "%s.%s.PC.%.16llx.STACK.%.16llx.ADDR.%.16llx.%s.%s",
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000203 arch_sigs[termsig].descr,
204 exception_to_string(fuzzer->exception), fuzzer->pc,
205 fuzzer->backtrace, fuzzer->access, fuzzer->origFileName, hfuzz->fileExtn);
groebert@google.comb857deb2010-10-21 19:09:29 +0000206 } else {
groebert@google.comb857deb2010-10-21 19:09:29 +0000207
groebert@google.com1bd4c212013-06-19 11:13:56 +0000208 char localtmstr[PATH_MAX];
209 util_getLocalTime("%F.%H.%M.%S", localtmstr, sizeof(localtmstr));
210
211 snprintf(newname, sizeof(newname),
212 "%s.%s.PC.%.16llx.STACK.%.16llx.ADDR.%.16llx.TIME.%s.PID.%.5d.%s.%s",
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000213 arch_sigs[termsig].descr,
214 exception_to_string(fuzzer->exception), fuzzer->pc,
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000215 fuzzer->backtrace, fuzzer->access, localtmstr, fuzzer->pid, fuzzer->origFileName,
216 hfuzz->fileExtn);
groebert@google.com1bd4c212013-06-19 11:13:56 +0000217 }
218
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000219 if (link(fuzzer->fileName, newname) == 0) {
groebert@google.com43216c82015-02-13 11:11:38 +0000220 LOGMSG(l_INFO, "Ok, that's interesting, saved '%s' as '%s'", fuzzer->fileName, newname);
groebert@google.com1bd4c212013-06-19 11:13:56 +0000221 } else {
222 if (errno == EEXIST) {
223 LOGMSG(l_INFO, "It seems that '%s' already exists, skipping", newname);
224 } else {
groebert@google.com43216c82015-02-13 11:11:38 +0000225 LOGMSG_P(l_ERROR, "Couldn't link '%s' to '%s'", fuzzer->fileName, newname);
groebert@google.comb857deb2010-10-21 19:09:29 +0000226 }
robert.swiecki3bb518c2010-10-14 00:48:24 +0000227 }
groebert@google.comb857deb2010-10-21 19:09:29 +0000228
robert.swiecki3bb518c2010-10-14 00:48:24 +0000229 return true;
230}
231
232bool arch_launchChild(honggfuzz_t * hfuzz, char *fileName)
233{
robert.swiecki3bb518c2010-10-14 00:48:24 +0000234#define ARGS_MAX 512
235 char *args[ARGS_MAX + 2];
236
237 int x;
238
239 for (x = 0; x < ARGS_MAX && hfuzz->cmdline[x]; x++) {
robert.swiecki@gmail.coma0d87142015-02-14 13:11:18 +0000240 if (!hfuzz->fuzzStdin && strcmp(hfuzz->cmdline[x], _HF_FILE_PLACEHOLDER) == 0) {
robert.swiecki3bb518c2010-10-14 00:48:24 +0000241 args[x] = fileName;
242 } else {
243 args[x] = hfuzz->cmdline[x];
244 }
245 }
246
247 args[x++] = NULL;
248
249 LOGMSG(l_DEBUG, "Launching '%s' on file '%s'", args[0], fileName);
250
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000251 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000252 * Get child's bootstrap port.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000253 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000254 mach_port_t child_bootstrap = MACH_PORT_NULL;
255 if (task_get_bootstrap_port(mach_task_self(), &child_bootstrap) != KERN_SUCCESS) {
256 return false;
257 }
258
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000259 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000260 * Get exception port.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000261 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000262 mach_port_t exception_port = MACH_PORT_NULL;
263
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000264 if (bootstrap_look_up(child_bootstrap, g_service_name, &exception_port)
265 != KERN_SUCCESS) {
groebert@google.com1bd4c212013-06-19 11:13:56 +0000266 return false;
267 }
268
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000269 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000270 * Here we register the exception port in the child
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000271 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000272 if (task_set_exception_ports(mach_task_self(),
273 EXC_MASK_CRASH,
274 exception_port,
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000275 EXCEPTION_STATE_IDENTITY |
276 MACH_EXCEPTION_CODES, MACHINE_THREAD_STATE) != KERN_SUCCESS) {
groebert@google.com1bd4c212013-06-19 11:13:56 +0000277 return false;
278 }
279
robert.swiecki3bb518c2010-10-14 00:48:24 +0000280 /*
281 * Set timeout (prof), real timeout (2*prof), and rlimit_cpu (2*prof)
282 */
283 if (hfuzz->tmOut) {
284 struct itimerval it;
285
286 /*
287 * The hfuzz->tmOut is real CPU usage time...
288 */
289 it.it_value.tv_sec = hfuzz->tmOut;
290 it.it_value.tv_usec = 0;
291 it.it_interval.tv_sec = 0;
292 it.it_interval.tv_usec = 0;
293 if (setitimer(ITIMER_PROF, &it, NULL) == -1) {
294 LOGMSG_P(l_ERROR, "Couldn't set the ITIMER_PROF timer");
295 return false;
296 }
297
298 /*
299 * ...so, if a process sleeps, this one should
300 * trigger a signal...
301 */
302 it.it_value.tv_sec = hfuzz->tmOut * 2UL;
303 it.it_value.tv_usec = 0;
304 it.it_interval.tv_sec = 0;
305 it.it_interval.tv_usec = 0;
306 if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
307 LOGMSG_P(l_ERROR, "Couldn't set the ITIMER_REAL timer");
308 return false;
309 }
310
311 /*
312 * ..if a process sleeps and catches SIGPROF/SIGALRM
313 * rlimits won't help either
314 */
315 struct rlimit rl;
316
317 rl.rlim_cur = hfuzz->tmOut * 2;
318 rl.rlim_max = hfuzz->tmOut * 2;
319 if (setrlimit(RLIMIT_CPU, &rl) == -1) {
320 LOGMSG_P(l_ERROR, "Couldn't enforce the RLIMIT_CPU resource limit");
321 return false;
322 }
323 }
324
325 /*
326 * The address space limit. If big enough - roughly the size of RAM used
327 */
328 if (hfuzz->asLimit) {
329 struct rlimit rl;
330
331 rl.rlim_cur = hfuzz->asLimit * 1024UL * 1024UL;
332 rl.rlim_max = hfuzz->asLimit * 1024UL * 1024UL;
333 if (setrlimit(RLIMIT_AS, &rl) == -1) {
334 LOGMSG_P(l_DEBUG, "Couldn't encforce the RLIMIT_AS resource limit, ignoring");
335 }
336 }
337
338 if (hfuzz->nullifyStdio) {
339 util_nullifyStdio();
340 }
341
342 if (hfuzz->fuzzStdin) {
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000343 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000344 * Uglyyyyyy ;)
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000345 */
robert.swiecki3bb518c2010-10-14 00:48:24 +0000346 if (!util_redirectStdin(fileName)) {
347 return false;
348 }
349 }
350
robert.swiecki@gmail.com7e3c48c2015-03-04 02:44:43 +0000351 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
352 char *env = strdupa(hfuzz->envs[i]);
353 char *eq = strchr(env, '=');
354 if (eq == NULL) {
355 continue;
356 }
357 *eq = '\0';
358 setenv(env, eq + 1, 1);
359 }
360
robert.swiecki3bb518c2010-10-14 00:48:24 +0000361 execvp(args[0], args);
362
363 util_recoverStdio();
364 LOGMSG(l_FATAL, "Failed to create new '%s' process", args[0]);
365 return false;
366}
367
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000368void arch_reapChild(honggfuzz_t * hfuzz, fuzzer_t * fuzzer)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000369{
groebert@google.com06d93802013-09-16 12:06:40 +0000370 /*
371 * First check manually if we have expired childs
372 */
373
groebert@google.com43216c82015-02-13 11:11:38 +0000374 double diff = difftime(time(NULL), fuzzer->timeStarted);
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000375 if (diff > (double)hfuzz->tmOut) {
376 LOGMSG(l_WARN,
377 "Process pid %d is overdue (%f seconds, max %f seconds %f), sending a SIGKILL",
groebert@google.com43216c82015-02-13 11:11:38 +0000378 fuzzer->pid, diff, (double)hfuzz->tmOut);
379 kill(fuzzer->pid, SIGKILL);
groebert@google.com06d93802013-09-16 12:06:40 +0000380 }
381
382 /*
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000383 * Now check for signals using wait4
groebert@google.com06d93802013-09-16 12:06:40 +0000384 */
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000385
robert.swiecki@gmail.com65cfa1c2015-02-11 15:59:28 +0000386 for (;;) {
387 int status = 0;
groebert@google.com43216c82015-02-13 11:11:38 +0000388 while (wait4(fuzzer->pid, &status, WUNTRACED, NULL) != fuzzer->pid) ;
robert.swiecki@gmail.com65cfa1c2015-02-11 15:59:28 +0000389 LOGMSG(l_DEBUG, "Process (pid %d) came back with status %d", fuzzer->pid, status);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000390
groebert@google.com43216c82015-02-13 11:11:38 +0000391 if (arch_analyzeSignal(hfuzz, status, fuzzer)) {
robert.swiecki@gmail.com65cfa1c2015-02-11 15:59:28 +0000392 return;
393 }
robert.swiecki3bb518c2010-10-14 00:48:24 +0000394 }
robert.swiecki@gmail.com751f6862015-02-11 15:03:26 +0000395}
robert.swiecki28cba5c2011-06-22 01:38:55 +0000396
groebert@google.com1bd4c212013-06-19 11:13:56 +0000397void *wait_for_exception()
398{
399 while (1) {
400 mach_msg_server_once(mach_exc_server, 4096, g_exception_port, MACH_MSG_OPTION_NONE);
401 }
402}
403
404/*
405 * Called once before fuzzing starts. Prepare mach ports for attaching crash reporter.
406 */
robert.swiecki@gmail.com6d6f7562015-02-17 22:18:51 +0000407bool arch_archInit(honggfuzz_t * hfuzz)
robert.swiecki28cba5c2011-06-22 01:38:55 +0000408{
groebert@google.com1bd4c212013-06-19 11:13:56 +0000409 char plist[PATH_MAX];
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000410 snprintf(plist, sizeof(plist), "/Users/%s/Library/Preferences/com.apple.DebugSymbols.plist",
411 getlogin());
groebert@google.com1bd4c212013-06-19 11:13:56 +0000412
413 if (files_exists(plist)) {
414 LOGMSG(l_WARN,
415 "honggfuzz won't work if DBGShellCommands are set in ~/Library/Preferences/com.apple.DebugSymbols.plist");
416 }
417
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000418 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000419 * Allocate exception port.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000420 */
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000421 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &g_exception_port) !=
422 KERN_SUCCESS) {
groebert@google.com1bd4c212013-06-19 11:13:56 +0000423 return false;
424 }
425
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000426 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000427 * Insert exception receive port.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000428 */
429 if (mach_port_insert_right
430 (mach_task_self(), g_exception_port, g_exception_port,
431 MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) {
groebert@google.com1bd4c212013-06-19 11:13:56 +0000432 return false;
433 }
434
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000435 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000436 * Get bootstrap port.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000437 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000438 mach_port_t bootstrap = MACH_PORT_NULL;
439 if (task_get_bootstrap_port(mach_task_self(), &bootstrap) != KERN_SUCCESS) {
440 return false;
441 }
442
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000443 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000444 * Generate and register exception port service.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000445 */
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000446 snprintf(g_service_name, sizeof(g_service_name), "com.google.code.honggfuzz.%d",
447 util_rndGet(0, 999999));
groebert@google.com76afff92013-06-20 14:43:41 +0000448 if (bootstrap_check_in(bootstrap, g_service_name, &g_exception_port) != KERN_SUCCESS) {
groebert@google.com1bd4c212013-06-19 11:13:56 +0000449 return false;
450 }
451
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000452 /*
453 * Create a collection thread to catch the exceptions from the
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000454 * children
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000455 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000456 pthread_t exception_thread;
457
458 if (pthread_create(&exception_thread, NULL, wait_for_exception, 0)) {
459 LOGMSG(l_FATAL, "Parent: could not create thread to wait for child's exception");
460 return false;
461 }
462
463 if (pthread_detach(exception_thread)) {
464 LOGMSG(l_FATAL, "Parent: could not detach thread to wait for child's exception");
465 return false;
466 }
467
robert.swiecki28cba5c2011-06-22 01:38:55 +0000468 return true;
469}
groebert@google.com1bd4c212013-06-19 11:13:56 +0000470
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000471/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000472 * Write the crash report to DEBUG
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000473 */
474void
475write_crash_report(thread_port_t thread,
476 task_port_t task,
477 exception_type_t exception,
478 mach_exception_data_t code,
479 mach_msg_type_number_t code_count,
480 int *flavor, thread_state_t in_state, mach_msg_type_number_t in_state_count)
groebert@google.com1bd4c212013-06-19 11:13:56 +0000481{
482
483 NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
484 CrashReport *_crashReport = nil;
485
groebert@google.com43216c82015-02-13 11:11:38 +0000486/* *INDENT-OFF* */
487 _crashReport = [[CrashReport alloc] initWithTask:task
488 exceptionType:exception
489 exceptionCode:code
490 exceptionCodeCount:code_count
491 thread:thread
492 threadStateFlavor:*flavor
493 threadState:(thread_state_t)in_state
494 threadStateCount:in_state_count];
495/* *INDENT-OFF* */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000496
497 NSString *crashDescription =[_crashReport description];
498 char *description = (char *)[crashDescription UTF8String];
499
500 LOGMSG(l_DEBUG, "CrashReport: %s", description);
501
502 [_crashReport release];
503 [pool drain];
504}
505
506/* Hash the callstack in an unique way */
507uint64_t hash_callstack(thread_port_t thread,
508 task_port_t task,
509 exception_type_t exception,
510 mach_exception_data_t code,
511 mach_msg_type_number_t code_count,
groebert@google.com43216c82015-02-13 11:11:38 +0000512 int *flavor,
513 thread_state_t in_state,
514 mach_msg_type_number_t in_state_count) {
groebert@google.com1bd4c212013-06-19 11:13:56 +0000515
516 NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
517 CrashReport *_crashReport = nil;
518
groebert@google.com43216c82015-02-13 11:11:38 +0000519/* *INDENT-OFF* */
520 _crashReport = [[CrashReport alloc] initWithTask:task
521 exceptionType:exception
522 exceptionCode:code
523 exceptionCodeCount:code_count
524 thread:thread
525 threadStateFlavor:*flavor
526 threadState:(thread_state_t)in_state
527 threadStateCount:in_state_count];
528/* *INDENT-ON* */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000529
530 NSString *crashDescription =[_crashReport description];
531 char *description = (char *)[crashDescription UTF8String];
532
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000533 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000534 * The callstack begins with the following word
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000535 */
groebert@google.com06d93802013-09-16 12:06:40 +0000536 char *callstack = strstr(description, "Crashed:");
groebert@google.com1bd4c212013-06-19 11:13:56 +0000537
538 if (callstack == NULL) {
539 LOGMSG(l_FATAL, "Could not find callstack in crash report %s", description);
540 }
541
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000542 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000543 * Scroll forward to the next newline
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000544 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000545 char *callstack_start = strstr(callstack, "\n");
546
547 if (callstack_start == NULL) {
548 LOGMSG(l_FATAL, "Could not find callstack start in crash report %s", description);
549 }
550
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000551 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000552 * Skip the newline
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000553 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000554 callstack_start++;
555
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000556 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000557 * Determine the end of the callstack
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000558 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000559 char *callstack_end = strstr(callstack, "\n\nThread");
560
561 if (callstack_end == NULL) {
562 LOGMSG(l_FATAL, "Could not find callstack end in crash report %s", description);
563 }
564
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000565 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000566 * Make sure it's NULL-terminated
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000567 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000568 *callstack_end = '\0';
569
570 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000571 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000572 * For each line, we only take the last three nibbles from the
573 * address.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000574 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000575 * Sample output:
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000576 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000577 * 0 libsystem_kernel.dylib 0x00007fff80514d46 __kill + 10 1
578 * libsystem_c.dylib 0x00007fff85731ec0 __abort + 193 2
579 * libsystem_c.dylib 0x00007fff85732d17 __stack_chk_fail + 195 3
580 * stack_buffer_overflow64-stripped 0x000000010339def5 0x10339d000 +
581 * 3829 4 ??? 0x4141414141414141 0 + 4702111234474983745
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000582 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000583 * 0 libsystem_kernel.dylib 0x00007fff80514d46 __kill + 10 1
584 * libsystem_c.dylib 0x00007fff85731ec0 __abort + 193 2
585 * libsystem_c.dylib 0x00007fff85732d17 __stack_chk_fail + 195 3
586 * stack_buffer_overflow64 0x0000000108f41ef5 main + 133 4 ???
587 * 0x4141414141414141 0 + 4702111234474983745
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000588 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000589 * 0 libsystem_kernel.dylib 0x940023ba __kill + 10 1
590 * libsystem_kernel.dylib 0x940014bc kill$UNIX2003 + 32 2
591 * libsystem_c.dylib 0x926f362e __abort + 246 3 libsystem_c.dylib
592 * 0x926c2b60 __chk_fail + 49 4 libsystem_c.dylib 0x926c2bf9
593 * __memset_chk + 53 5 stack_buffer_overflow32-stripped 0x00093ee5
594 * 0x93000 + 3813 6 libdyld.dylib 0x978c6725 start + 1
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000595 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000596 * 0 libsystem_kernel.dylib 0x940023ba __kill + 10 1
597 * libsystem_kernel.dylib 0x940014bc kill$UNIX2003 + 32 2
598 * libsystem_c.dylib 0x926f362e __abort + 246 3 libsystem_c.dylib
599 * 0x926c2b60 __chk_fail + 49 4 libsystem_c.dylib 0x926c2bf9
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000600 * __memset_chk + 53 5 stack_buffer_overflow32 0x0003cee5 main + 117 6
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000601 * libdyld.dylib 0x978c6725 start + 1
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000602 *
groebert@google.com1bd4c212013-06-19 11:13:56 +0000603 */
604
605 uint64_t hash = 0;
606 char *pos = callstack_start;
607
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000608 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000609 * Go through each line until we run out of lines
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000610 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000611 while (strstr(pos, "\t") != NULL) {
612 /*
613 * Format: dylib spaces tab address space symbol space plus space offset
614 * Scroll pos forward to the last three nibbles of the address.
615 */
616 if ((pos = strstr(pos, "\t")) == NULL)
617 break;
618 if ((pos = strstr(pos, " ")) == NULL)
619 break;
620 pos = pos - 3;
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000621 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000622 * Hash the last three nibbles
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000623 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000624 hash ^= util_hash(pos, 3);
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000625 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000626 * Scroll pos one forward to skip the current tab
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000627 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000628 pos++;
629 }
630
groebert@google.com20e368f2015-02-13 14:19:25 +0000631 LOGMSG(l_DEBUG, "Callstack hash %u", hash);
groebert@google.com1bd4c212013-06-19 11:13:56 +0000632
633 [_crashReport release];
634 [pool drain];
635
636 return hash;
637}
638
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000639kern_return_t
640catch_mach_exception_raise(mach_port_t exception_port,
641 mach_port_t thread,
642 mach_port_t task,
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +0000643 exception_type_t exception, mach_exception_data_t code,
644 mach_msg_type_number_t codeCnt)
groebert@google.comc659c0e2015-02-13 11:17:00 +0000645{
groebert@google.com1bd4c212013-06-19 11:13:56 +0000646 LOGMSG(l_FATAL, "This function should never get called");
647 return KERN_SUCCESS;
648}
649
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000650kern_return_t
651catch_mach_exception_raise_state(mach_port_t exception_port,
652 exception_type_t exception,
653 const mach_exception_data_t code,
654 mach_msg_type_number_t codeCnt,
655 int *flavor,
656 const thread_state_t old_state,
657 mach_msg_type_number_t old_stateCnt,
658 thread_state_t new_state, mach_msg_type_number_t * new_stateCnt)
groebert@google.comc659c0e2015-02-13 11:17:00 +0000659{
groebert@google.com1bd4c212013-06-19 11:13:56 +0000660 LOGMSG(l_FATAL, "This function should never get called");
661 return KERN_SUCCESS;
662}
663
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000664kern_return_t catch_mach_exception_raise_state_identity( __attribute__ ((unused))
665 exception_port_t exception_port,
666 thread_port_t thread,
groebert@google.com1bd4c212013-06-19 11:13:56 +0000667 task_port_t task,
668 exception_type_t exception,
669 mach_exception_data_t code,
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000670 mach_msg_type_number_t
671 code_count, int *flavor,
672 thread_state_t in_state,
673 mach_msg_type_number_t
674 in_state_count,
groebert@google.com1bd4c212013-06-19 11:13:56 +0000675 thread_state_t out_state,
groebert@google.comc659c0e2015-02-13 11:17:00 +0000676 mach_msg_type_number_t * out_state_count)
677{
groebert@google.com1bd4c212013-06-19 11:13:56 +0000678 if (exception != EXC_CRASH) {
679 LOGMSG(l_FATAL, "Got non EXC_CRASH! This should not happen.");
680 }
681
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000682 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000683 * We will save our results to the honggfuzz_t global
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000684 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000685 pid_t pid;
686 pid_for_task(task, &pid);
687 LOGMSG(l_DEBUG, "Crash of pid %d", pid);
688
groebert@google.com20e368f2015-02-13 14:19:25 +0000689 fuzzer_t *fuzzer = &g_fuzzer_crash_information[pid];
robert.swiecki@gmail.comc6d28752014-01-08 12:02:37 +0000690
groebert@google.com1bd4c212013-06-19 11:13:56 +0000691 /*
692 * Get program counter.
693 * Cast to void* in order to silence the alignment warnings
694 */
695
696 x86_thread_state_t *platform_in_state = ((x86_thread_state_t *) (void *)in_state);
697
698 if (x86_THREAD_STATE32 == platform_in_state->tsh.flavor) {
groebert@google.com20e368f2015-02-13 14:19:25 +0000699 fuzzer->pc = platform_in_state->uts.ts32.__eip;
groebert@google.com1bd4c212013-06-19 11:13:56 +0000700 } else {
groebert@google.com20e368f2015-02-13 14:19:25 +0000701 fuzzer->pc = platform_in_state->uts.ts64.__rip;
groebert@google.com1bd4c212013-06-19 11:13:56 +0000702 }
703
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000704 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000705 * Get the exception type
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000706 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000707
708 exception_type_t exception_type = ((code[0] >> 20) & 0x0F);
709
710 if (exception_type == 0) {
711 exception_type = EXC_CRASH;
712 }
713
groebert@google.com20e368f2015-02-13 14:19:25 +0000714 fuzzer->exception = exception_type;
groebert@google.com1bd4c212013-06-19 11:13:56 +0000715
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000716 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000717 * Get the access address.
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000718 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000719
720 mach_exception_data_type_t exception_data[2];
721 memcpy(exception_data, code, sizeof(exception_data));
722 exception_data[0] = (code[0] & ~(0x00000000FFF00000));
723 exception_data[1] = code[1];
724
725 mach_exception_data_type_t access_address = exception_data[1];
groebert@google.com20e368f2015-02-13 14:19:25 +0000726 fuzzer->access = (uint64_t) access_address;
groebert@google.com1bd4c212013-06-19 11:13:56 +0000727
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000728 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000729 * Get a hash of the callstack
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000730 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000731
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000732 uint64_t hash = hash_callstack(thread, task, exception, code, code_count, flavor,
733 in_state, in_state_count);
groebert@google.com1bd4c212013-06-19 11:13:56 +0000734
groebert@google.com20e368f2015-02-13 14:19:25 +0000735 fuzzer->backtrace = hash;
groebert@google.com1bd4c212013-06-19 11:13:56 +0000736
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000737 /*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000738 * Cleanup
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000739 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000740
741 if (mach_port_deallocate(mach_task_self(), task) != KERN_SUCCESS) {
742 LOGMSG(l_WARN, "Exception Handler: Could not deallocate task");
743 }
744
745 if (mach_port_deallocate(mach_task_self(), thread) != KERN_SUCCESS) {
746 LOGMSG(l_WARN, "Exception Handler: Could not deallocate thread");
747 }
748
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +0000749 return KERN_SUCCESS; // KERN_SUCCESS indicates that this should
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000750 // not be forwarded to other crash
751 // handlers
groebert@google.com1bd4c212013-06-19 11:13:56 +0000752}