blob: 554dbf339cc537438f4941709b0259497b0f524e [file] [log] [blame]
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +02001#include "sanitizers.h"
2
3#include <ctype.h>
4#include <dirent.h>
5#include <inttypes.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <sys/mman.h>
10#include <sys/stat.h>
11#include <sys/types.h>
12
Robert Swiecki15801e82018-11-20 15:59:25 +010013#include "cmdline.h"
Robert Swiecki246af3e2018-01-05 14:56:32 +010014#include "libhfcommon/common.h"
15#include "libhfcommon/files.h"
16#include "libhfcommon/log.h"
17#include "libhfcommon/util.h"
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020018
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020019/*
Anestis Bechtsoudisc2722ee2017-01-29 09:40:03 +020020 * All clang sanitizers, except ASan, can be activated for target binaries
21 * with or without the matching runtime library (libcompiler_rt). If runtime
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020022 * libraries are included in target fuzzing environment, we can benefit from the
23 * various Die() callbacks and abort/exit logic manipulation. However, some
Anestis Bechtsoudisc2722ee2017-01-29 09:40:03 +020024 * setups (e.g. Android production ARM/ARM64 devices) enable sanitizers, such as
25 * UBSan, without the runtime libraries. As such, their default ftrap is activated
26 * which is for most cases a SIGABRT. For these cases end-user needs to enable
27 * SIGABRT monitoring flag, otherwise these crashes will be missed.
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020028 *
Anestis Bechtsoudisc2722ee2017-01-29 09:40:03 +020029 * Normally SIGABRT is not a wanted signal to monitor for Android, since it produces
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020030 * lots of useless crashes due to way Android process termination hacks work. As
31 * a result the sanitizer's 'abort_on_error' flag cannot be utilized since it
32 * invokes abort() internally. In order to not lose crashes a custom exitcode can
33 * be registered and monitored. Since exitcode is a global flag, it's assumed
34 * that target is compiled with only one sanitizer type enabled at a time.
35 *
Anestis Bechtsoudisc2722ee2017-01-29 09:40:03 +020036 * For cases where clang runtime library linking is not an option, SIGABRT should
37 * be monitored even for noisy targets, such as the Android OS, since no viable
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020038 * alternative exists.
Anestis Bechtsoudis523fccb2016-12-28 12:11:13 +020039 *
40 * There might be cases where ASan instrumented targets crash while generating
41 * reports for detected errors (inside __asan_report_error() proc). Under such
42 * scenarios target fails to exit or SIGABRT (AsanDie() proc) as defined in
43 * ASAN_OPTIONS flags, leaving garbage logs. An attempt is made to parse such
44 * logs for cases where enough data are written to identify potentially missed
45 * crashes. If ASan internal error results into a SIGSEGV being raised, it
46 * will get caught from ptrace API, handling the discovered ASan internal crash.
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020047 */
48
49/* 'log_path' output directory for sanitizer reports */
Robert Swieckid0fa62c2017-09-28 18:11:05 +020050#define kSANLOGDIR "log_path="
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020051
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020052/* Raise SIGABRT on error or continue with exitcode logic */
Robert Swieckid0fa62c2017-09-28 18:11:05 +020053#define kABORT_ENABLED "abort_on_error=1"
54#define kABORT_DISABLED "abort_on_error=0"
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020055
56/*
57 * Common sanitizer flags
58 *
59 * symbolize: Disable symbolication since it changes logs (which are parsed) format
60 */
Robert Swieckid0fa62c2017-09-28 18:11:05 +020061#define kSAN_COMMON "symbolize=0"
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020062
63/* --{ ASan }-- */
64/*
65 *Sanitizer specific flags (notice that if enabled 'abort_on_error' has priority
66 * over exitcode')
67 */
Robert Swieckid50ed422017-11-13 23:32:26 +010068#define kASAN_COMMON_OPTS \
69 "allow_user_segv_handler=1:" \
70 "handle_segv=0:" \
Robert Swiecki89d444a2018-01-11 23:45:19 +010071 "allocator_may_return_null=1:" kSAN_COMMON ":exitcode=" HF_XSTR(HF_SAN_EXIT_CODE)
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020072/* Platform specific flags */
73#if defined(__ANDROID__)
74/*
75 * start_deactivated: Enable on Android to reduce memory usage (useful when not all
76 * target's DSOs are compiled with sanitizer enabled
77 */
Robert Swieckid0fa62c2017-09-28 18:11:05 +020078#define kASAN_OPTS kASAN_COMMON_OPTS ":start_deactivated=1"
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020079#else
Robert Swieckid0fa62c2017-09-28 18:11:05 +020080#define kASAN_OPTS kASAN_COMMON_OPTS
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020081#endif
82
83/* --{ UBSan }-- */
Robert Swieckid0fa62c2017-09-28 18:11:05 +020084#define kUBSAN_OPTS kSAN_COMMON ":exitcode=" STR(HF_SAN_EXIT_CODE)
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020085
86/* --{ MSan }-- */
Robert Swiecki98e23372019-01-30 11:50:18 +010087#define kMSAN_OPTS \
88 kSAN_COMMON ":exit_code=" STR(HF_SAN_EXIT_CODE) ":" \
Robert Swiecki0b566112017-10-17 17:39:07 +020089 "wrap_signals=0:print_stats=1"
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020090
Robert Swieckib4e13c02017-02-10 03:59:40 +010091/* If no sanitzer support was requested, simply make it use abort() on errors */
Robert Swieckiba3ee412018-01-04 14:10:49 +010092#define kSAN_REGULAR \
93 "abort_on_error=1:handle_segv=0:handle_sigbus=0:handle_abort=0:" \
94 "handle_sigill=0:handle_sigfpe=0:allocator_may_return_null=1:" \
Robert Swiecki0b418dd2018-11-19 16:08:36 +010095 "symbolize=1:detect_leaks=0:disable_coredump=0:" \
96 "detect_odr_violation=0"
Robert Swieckib4e13c02017-02-10 03:59:40 +010097
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +020098/*
99 * If the program ends with a signal that ASan does not handle (or can not
100 * handle at all, like SIGKILL), coverage data will be lost. This is a big
101 * problem on Android, where SIGKILL is a normal way of evicting applications
102 * from memory. With 'coverage_direct=1' coverage data is written to a
103 * memory-mapped file as soon as it collected. Non-Android targets can disable
104 * coverage direct when more coverage data collection methods are implemented.
105 */
Robert Swieckid0fa62c2017-09-28 18:11:05 +0200106#define kSAN_COV_OPTS "coverage=1:coverage_direct=1"
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200107
Robert Swiecki97d88932018-01-10 19:29:34 +0100108static void sanitizers_AddFlag(honggfuzz_t* hfuzz, const char* env, char* buf, size_t buflen) {
Robert Swieckia5b918a2018-03-07 23:59:53 +0100109 const char* abortFlag = hfuzz->cfg.monitorSIGABRT ? kABORT_ENABLED : kABORT_DISABLED;
Robert Swiecki754d1692018-11-19 17:24:27 +0100110 if (getenv(env)) {
111 LOG_W("The '%s' envar is already set. Not overriding it!", env);
112 return;
113 }
Robert Swiecki97d88932018-01-10 19:29:34 +0100114
Robert Swieckia5b918a2018-03-07 23:59:53 +0100115 if (!hfuzz->sanitizer.enable) {
Robert Swiecki97d88932018-01-10 19:29:34 +0100116 snprintf(buf, buflen, "%s=%s", env, kSAN_REGULAR);
Robert Swiecki97d88932018-01-10 19:29:34 +0100117 } else {
118 snprintf(buf, buflen, "%s=%s:%s:%s%s/%s", env, kASAN_OPTS, abortFlag, kSANLOGDIR,
119 hfuzz->io.workDir, kLOGPREFIX);
120 }
Robert Swieckib165bfc2018-01-22 16:00:07 +0100121 /*
122 * It will make ASAN to start background thread to check RSS mem use, which
123 * will prevent the NetDrvier from using unshare(CLONE_NEWNET), which cannot
124 * be used in multi-threaded contexts
125 */
126 if (!hfuzz->exe.netDriver && hfuzz->exe.rssLimit) {
127 util_ssnprintf(buf, buflen, ":soft_rss_limit_mb=%" PRId64, hfuzz->exe.rssLimit);
Robert Swiecki5cc1f7b2018-01-16 20:55:43 +0100128 }
Robert Swiecki97d88932018-01-10 19:29:34 +0100129
Robert Swiecki15801e82018-11-20 15:59:25 +0100130 cmdlineAddEnv(hfuzz, buf);
Robert Swieckicec31c92017-12-27 19:23:54 +0100131 LOG_D("%s", env);
Robert Swiecki528935c2017-12-27 18:51:54 +0100132}
133
Robert Swieckid50ed422017-11-13 23:32:26 +0100134bool sanitizers_Init(honggfuzz_t* hfuzz) {
Robert Swieckie2a05ed2018-01-10 19:32:19 +0100135 static char asanOpts[4096];
Robert Swiecki97d88932018-01-10 19:29:34 +0100136 sanitizers_AddFlag(hfuzz, "ASAN_OPTIONS", asanOpts, sizeof(asanOpts));
Robert Swieckie2a05ed2018-01-10 19:32:19 +0100137 static char ubsanOpts[4096];
Robert Swiecki97d88932018-01-10 19:29:34 +0100138 sanitizers_AddFlag(hfuzz, "UBSAN_OPTIONS", ubsanOpts, sizeof(ubsanOpts));
Robert Swieckie2a05ed2018-01-10 19:32:19 +0100139 static char msanOpts[4096];
Robert Swiecki97d88932018-01-10 19:29:34 +0100140 sanitizers_AddFlag(hfuzz, "MSAN_OPTIONS", msanOpts, sizeof(msanOpts));
Robert Swieckie2a05ed2018-01-10 19:32:19 +0100141 static char lsanOpts[4096];
Robert Swiecki97d88932018-01-10 19:29:34 +0100142 sanitizers_AddFlag(hfuzz, "LSAN_OPTIONS", lsanOpts, sizeof(lsanOpts));
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200143
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200144 return true;
145}