blob: 5e9a37dcff488a3eed3e61dae4a4f222dde7606e [file] [log] [blame]
Kostya Serebryany16d03bd2015-03-30 22:09:51 +00001//===- FuzzerDFSan.cpp - DFSan-based fuzzer mutator -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// DataFlowSanitizer (DFSan) is a tool for
10// generalised dynamic data flow (taint) analysis:
11// http://clang.llvm.org/docs/DataFlowSanitizer.html .
12//
13// This file implements a mutation algorithm based on taint
14// analysis feedback from DFSan.
15//
16// The approach has some similarity to "Taint-based Directed Whitebox Fuzzing"
17// by Vijay Ganesh & Tim Leek & Martin Rinard:
18// http://dspace.mit.edu/openaccess-disseminate/1721.1/59320,
19// but it uses a full blown LLVM IR taint analysis and separate instrumentation
20// to analyze all of the "attack points" at once.
21//
22// Workflow:
23// * lib/Fuzzer/Fuzzer*.cpp is compiled w/o any instrumentation.
24// * The code under test is compiled with DFSan *and* with special extra hooks
25// that are inserted before dfsan. Currently supported hooks:
26// - __sanitizer_cov_trace_cmp: inserted before every ICMP instruction,
27// receives the type, size and arguments of ICMP.
28// * Every call to HOOK(a,b) is replaced by DFSan with
29// __dfsw_HOOK(a, b, label(a), label(b)) so that __dfsw_HOOK
30// gets all the taint labels for the arguments.
31// * At the Fuzzer startup we assign a unique DFSan label
32// to every byte of the input string (Fuzzer::CurrentUnit) so that for any
33// chunk of data we know which input bytes it has derived from.
34// * The __dfsw_* functions (implemented in this file) record the
35// parameters (i.e. the application data and the corresponding taint labels)
36// in a global state.
37// * Fuzzer::MutateWithDFSan() tries to use the data recorded by __dfsw_*
38// hooks to guide the fuzzing towards new application states.
39// For example if 4 bytes of data that derive from input bytes {4,5,6,7}
40// are compared with a constant 12345 and the comparison always yields
41// the same result, we try to insert 12345, 12344, 12346 into bytes
42// {4,5,6,7} of the next fuzzed inputs.
43//
44// This code does not function when DFSan is not linked in.
45// Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer
46// we redeclare the dfsan_* interface functions as weak and check if they
47// are nullptr before calling.
48// If this approach proves to be useful we may add attribute(weak) to the
49// dfsan declarations in dfsan_interface.h
50//
51// This module is in the "proof of concept" stage.
52// It is capable of solving only the simplest puzzles
53// like test/dfsan/DFSanSimpleCmpTest.cpp.
54//===----------------------------------------------------------------------===//
55
56/* Example of manual usage:
57(
58 cd $LLVM/lib/Fuzzer/
59 clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp
60 clang++ -O0 -std=c++11 -fsanitize-coverage=3 \
61 -mllvm -sanitizer-coverage-experimental-trace-compares=1 \
Kostya Serebryany3befe942015-05-06 22:47:24 +000062 -fsanitize=dataflow \
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000063 test/dfsan/DFSanSimpleCmpTest.cpp Fuzzer*.o
64 ./a.out
65)
66*/
67
68#include "FuzzerInternal.h"
69#include <sanitizer/dfsan_interface.h>
70
Kostya Serebryanybeb24c32015-05-07 21:02:11 +000071#include <algorithm>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000072#include <cstring>
73#include <iostream>
74#include <unordered_map>
75
76extern "C" {
77__attribute__((weak))
78dfsan_label dfsan_create_label(const char *desc, void *userdata);
79__attribute__((weak))
80void dfsan_set_label(dfsan_label label, void *addr, size_t size);
81__attribute__((weak))
82void dfsan_add_label(dfsan_label label, void *addr, size_t size);
83__attribute__((weak))
84const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
Kostya Serebryanya407dde2015-05-07 00:11:33 +000085__attribute__((weak))
86dfsan_label dfsan_read_label(const void *addr, size_t size);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000087} // extern "C"
88
89namespace {
90
91// These values are copied from include/llvm/IR/InstrTypes.h.
92// We do not include the LLVM headers here to remain independent.
93// If these values ever change, an assertion in ComputeCmp will fail.
94enum Predicate {
95 ICMP_EQ = 32, ///< equal
96 ICMP_NE = 33, ///< not equal
97 ICMP_UGT = 34, ///< unsigned greater than
98 ICMP_UGE = 35, ///< unsigned greater or equal
99 ICMP_ULT = 36, ///< unsigned less than
100 ICMP_ULE = 37, ///< unsigned less or equal
101 ICMP_SGT = 38, ///< signed greater than
102 ICMP_SGE = 39, ///< signed greater or equal
103 ICMP_SLT = 40, ///< signed less than
104 ICMP_SLE = 41, ///< signed less or equal
105};
106
107template <class U, class S>
108bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
109 switch(CmpType) {
110 case ICMP_EQ : return Arg1 == Arg2;
111 case ICMP_NE : return Arg1 != Arg2;
112 case ICMP_UGT: return Arg1 > Arg2;
113 case ICMP_UGE: return Arg1 >= Arg2;
114 case ICMP_ULT: return Arg1 < Arg2;
115 case ICMP_ULE: return Arg1 <= Arg2;
116 case ICMP_SGT: return (S)Arg1 > (S)Arg2;
117 case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
118 case ICMP_SLT: return (S)Arg1 < (S)Arg2;
119 case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
120 default: assert(0 && "unsupported CmpType");
121 }
122 return false;
123}
124
125static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
126 uint64_t Arg2) {
127 if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
128 if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
129 if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
130 if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
131 assert(0 && "unsupported type size");
132 return true;
133}
134
135// As a simplification we use the range of input bytes instead of a set of input
136// bytes.
137struct LabelRange {
138 uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range.
139
140 LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
141
142 static LabelRange Join(LabelRange LR1, LabelRange LR2) {
143 if (LR1.Beg == LR1.End) return LR2;
144 if (LR2.Beg == LR2.End) return LR1;
145 return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
146 }
147 LabelRange &Join(LabelRange LR) {
148 return *this = Join(*this, LR);
149 }
150 static LabelRange Singleton(const dfsan_label_info *LI) {
151 uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
152 assert(Idx > 0);
153 return {(uint16_t)(Idx - 1), Idx};
154 }
155};
156
157std::ostream &operator<<(std::ostream &os, const LabelRange &LR) {
158 return os << "[" << LR.Beg << "," << LR.End << ")";
159}
160
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000161// For now, very simple: put Size bytes of Data at position Pos.
162struct TraceBasedMutation {
163 size_t Pos;
164 size_t Size;
165 uint64_t Data;
166};
167
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000168class DFSanState {
169 public:
170 DFSanState(const fuzzer::Fuzzer::FuzzingOptions &Options)
171 : Options(Options) {}
172
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000173 LabelRange GetLabelRange(dfsan_label L);
174 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
175 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
176 dfsan_label L2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000177
178 void StartTraceRecording() {
179 RecordingTraces = true;
180 Mutations.clear();
181 }
182
183 size_t StopTraceRecording() {
184 RecordingTraces = false;
185 std::random_shuffle(Mutations.begin(), Mutations.end());
186 return Mutations.size();
187 }
188
189 void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000190
191 private:
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000192 bool RecordingTraces = false;
193 std::vector<TraceBasedMutation> Mutations;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000194 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)] = {};
195 const fuzzer::Fuzzer::FuzzingOptions &Options;
196};
197
198LabelRange DFSanState::GetLabelRange(dfsan_label L) {
199 LabelRange &LR = LabelRanges[L];
200 if (LR.Beg < LR.End || L == 0)
201 return LR;
202 const dfsan_label_info *LI = dfsan_get_label_info(L);
203 if (LI->l1 || LI->l2)
204 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
205 return LR = LabelRange::Singleton(LI);
206}
207
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000208void DFSanState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) {
209 assert(Idx < Mutations.size());
210 auto &M = Mutations[Idx];
211 if (Options.Verbosity >= 3)
212 std::cerr << "TBM " << M.Pos << " " << M.Size << " " << M.Data << "\n";
213 if (M.Pos + M.Size > U->size()) return;
214 memcpy(U->data() + M.Pos, &M.Data, M.Size);
215}
216
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000217void DFSanState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
218 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
219 dfsan_label L2) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000220 if (!RecordingTraces) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000221 if (L1 == 0 && L2 == 0)
222 return; // Not actionable.
223 if (L1 != 0 && L2 != 0)
224 return; // Probably still actionable.
225 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000226 uint64_t Data = L1 ? Arg2 : Arg1;
227 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000228
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000229 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
230 Mutations.push_back({Pos, CmpSize, Data});
231 Mutations.push_back({Pos, CmpSize, Data + 1});
232 Mutations.push_back({Pos, CmpSize, Data - 1});
233 }
234
235 if (CmpSize > LR.End - LR.Beg)
236 Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data});
237
238
239 if (Options.Verbosity >= 3)
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000240 std::cerr << "DFSAN:"
241 << " PC " << std::hex << PC << std::dec
242 << " S " << CmpSize
243 << " T " << CmpType
244 << " A1 " << Arg1 << " A2 " << Arg2 << " R " << Res
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000245 << " L" << L1
246 << " L" << L2
247 << " R" << LR
248 << " MU " << Mutations.size()
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000249 << "\n";
250}
251
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000252static DFSanState *DFSan;
253
254} // namespace
255
256namespace fuzzer {
257
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000258void Fuzzer::StartTraceRecording() {
259 if (!DFSan) return;
260 DFSan->StartTraceRecording();
261}
262
263size_t Fuzzer::StopTraceRecording() {
264 if (!DFSan) return 0;
265 return DFSan->StopTraceRecording();
266}
267
268void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) {
269 assert(DFSan);
270 DFSan->ApplyTraceBasedMutation(Idx, U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000271}
272
273void Fuzzer::InitializeDFSan() {
274 if (!&dfsan_create_label || !Options.UseDFSan) return;
275 DFSan = new DFSanState(Options);
276 CurrentUnit.resize(Options.MaxLen);
277 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
278 dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
279 // We assume that no one else has called dfsan_create_label before.
280 assert(L == i + 1);
281 dfsan_set_label(L, &CurrentUnit[i], 1);
282 }
283}
284
285} // namespace fuzzer
286
287extern "C" {
288void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
289 uint64_t Arg2, dfsan_label L0,
290 dfsan_label L1, dfsan_label L2) {
291 assert(L0 == 0);
292 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
293 uint64_t CmpSize = (SizeAndType >> 32) / 8;
294 uint64_t Type = (SizeAndType << 32) >> 32;
295 DFSan->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
296}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000297
298void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
299 size_t n, dfsan_label s1_label,
300 dfsan_label s2_label, dfsan_label n_label) {
301 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000302 uint64_t S1 = 0, S2 = 0;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000303 // Simplification: handle only first 8 bytes.
304 memcpy(&S1, s1, std::min(n, sizeof(S1)));
305 memcpy(&S2, s2, std::min(n, sizeof(S2)));
306 dfsan_label L1 = dfsan_read_label(s1, n);
307 dfsan_label L2 = dfsan_read_label(s2, n);
308 DFSan->DFSanCmpCallback(PC, n, ICMP_EQ, S1, S2, L1, L2);
309}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000310
311void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
312 uint64_t Arg2) {
313 // This symbol will be present if dfsan is disabled on the given function.
314 // FIXME: implement poor man's taint analysis here (w/o dfsan).
315}
316
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000317} // extern "C"