about summary refs log tree commit diff
path: root/src/afl-fuzz-mutators.c
blob: 9788da49d7fc96b546b5f0989ec70b8134b8a4a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
   american fuzzy lop++ - custom mutators related routines
   -------------------------------------------------------

   Originally written by Shengtuo Hu

   Now maintained by  Marc Heuse <mh@mh-sec.de>,
                        Heiko Eißfeldt <heiko.eissfeldt@hexco.de> and
                        Andrea Fioraldi <andreafioraldi@gmail.com>

   Copyright 2016, 2017 Google Inc. All rights reserved.
   Copyright 2019-2020 AFLplusplus Project. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at:

     http://www.apache.org/licenses/LICENSE-2.0

   This is the real deal: the program takes an instrumented binary and
   attempts a variety of basic fuzzing tricks, paying close attention to
   how they affect the execution path.

 */

#include "afl-fuzz.h"

void load_custom_mutator(afl_state_t *, const char *);
#ifdef USE_PYTHON
void load_custom_mutator_py(afl_state_t *, const char *);
#endif

void setup_custom_mutator(afl_state_t *afl) {

  /* Try mutator library first */
  u8 *fn = getenv("AFL_CUSTOM_MUTATOR_LIBRARY");

  if (fn) {

    if (afl->limit_time_sig)
      FATAL(
          "MOpt and custom mutator are mutually exclusive. We accept pull "
          "requests that integrates MOpt with the optional mutators "
          "(custom/radamsa/redquenn/...).");

    load_custom_mutator(afl, fn);

    return;

  }

  /* Try Python module */
#ifdef USE_PYTHON
  u8 *module_name = getenv("AFL_PYTHON_MODULE");

  if (module_name) {

    if (afl->limit_time_sig)
      FATAL(
          "MOpt and Python mutator are mutually exclusive. We accept pull "
          "requests that integrates MOpt with the optional mutators "
          "(custom/radamsa/redquenn/...).");

    if (init_py_module(afl, module_name))
      FATAL("Failed to initialize Python module");

    load_custom_mutator_py(afl, module_name);

  }

#else
  if (getenv("AFL_PYTHON_MODULE"))
    FATAL("Your AFL binary was built without Python support");
#endif

}

void destroy_custom_mutator(afl_state_t *afl) {

  if (afl->mutator) {

    if (afl->mutator->dh)
      dlclose(afl->mutator->dh);
    else {

      /* Python mutator */
#ifdef USE_PYTHON
      finalize_py_module(afl);
#endif

    }

    ck_free(afl->mutator);

  }

}

void load_custom_mutator(afl_state_t *afl, const char *fn) {

  void *dh;
  afl->mutator = ck_alloc(sizeof(struct custom_mutator));

  afl->mutator->name = fn;
  ACTF("Loading custom mutator library from '%s'...", fn);

  dh = dlopen(fn, RTLD_NOW);
  if (!dh) FATAL("%s", dlerror());
  afl->mutator->dh = dh;

  /* Mutator */
  /* "afl_custom_init", optional for backward compatibility */
  afl->mutator->afl_custom_init = dlsym(dh, "afl_custom_init");
  if (!afl->mutator->afl_custom_init)
    WARNF("Symbol 'afl_custom_init' not found.");

  /* "afl_custom_fuzz" or "afl_custom_mutator", required */
  afl->mutator->afl_custom_fuzz = dlsym(dh, "afl_custom_fuzz");
  if (!afl->mutator->afl_custom_fuzz) {

    /* Try "afl_custom_mutator" for backward compatibility */
    WARNF("Symbol 'afl_custom_fuzz' not found. Try 'afl_custom_mutator'.");

    afl->mutator->afl_custom_fuzz = dlsym(dh, "afl_custom_mutator");
    if (!afl->mutator->afl_custom_fuzz)
      FATAL("Symbol 'afl_custom_mutator' not found.");

  }

  /* "afl_custom_pre_save", optional */
  afl->mutator->afl_custom_pre_save = dlsym(dh, "afl_custom_pre_save");
  if (!afl->mutator->afl_custom_pre_save)
    WARNF("Symbol 'afl_custom_pre_save' not found.");

  u8 notrim = 0;
  /* "afl_custom_init_trim", optional */
  afl->mutator->afl_custom_init_trim = dlsym(dh, "afl_custom_init_trim");
  if (!afl->mutator->afl_custom_init_trim)
    WARNF("Symbol 'afl_custom_init_trim' not found.");

  /* "afl_custom_trim", optional */
  afl->mutator->afl_custom_trim = dlsym(dh, "afl_custom_trim");
  if (!afl->mutator->afl_custom_trim)
    WARNF("Symbol 'afl_custom_trim' not found.");

  /* "afl_custom_post_trim", optional */
  afl->mutator->afl_custom_post_trim = dlsym(dh, "afl_custom_post_trim");
  if (!afl->mutator->afl_custom_post_trim)
    WARNF("Symbol 'afl_custom_post_trim' not found.");

  if (notrim) {

    afl->mutator->afl_custom_init_trim = NULL;
    afl->mutator->afl_custom_trim = NULL;
    afl->mutator->afl_custom_post_trim = NULL;
    WARNF(
        "Custom mutator does not implement all three trim APIs, standard "
        "trimming will be used.");

  }

  /* "afl_custom_havoc_mutation", optional */
  afl->mutator->afl_custom_havoc_mutation =
      dlsym(dh, "afl_custom_havoc_mutation");
  if (!afl->mutator->afl_custom_havoc_mutation)
    WARNF("Symbol 'afl_custom_havoc_mutation' not found.");

  /* "afl_custom_havoc_mutation", optional */
  afl->mutator->afl_custom_havoc_mutation_probability =
      dlsym(dh, "afl_custom_havoc_mutation_probability");
  if (!afl->mutator->afl_custom_havoc_mutation_probability)
    WARNF("Symbol 'afl_custom_havoc_mutation_probability' not found.");

  /* "afl_custom_queue_get", optional */
  afl->mutator->afl_custom_queue_get = dlsym(dh, "afl_custom_queue_get");
  if (!afl->mutator->afl_custom_queue_get)
    WARNF("Symbol 'afl_custom_queue_get' not found.");

  /* "afl_custom_queue_new_entry", optional */
  afl->mutator->afl_custom_queue_new_entry =
      dlsym(dh, "afl_custom_queue_new_entry");
  if (!afl->mutator->afl_custom_queue_new_entry)
    WARNF("Symbol 'afl_custom_queue_new_entry' not found");

  OKF("Custom mutator '%s' installed successfully.", fn);

  /* Initialize the custom mutator */
  if (afl->mutator->afl_custom_init)
    afl->mutator->afl_custom_init(afl, UR(afl, 0xFFFFFFFF));

}

u8 trim_case_custom(afl_state_t *afl, struct queue_entry *q, u8 *in_buf) {

  u8  needs_write = 0, fault = 0;
  u32 trim_exec = 0;
  u32 orig_len = q->len;

  if (afl->stage_name != afl->stage_name_buf)
    afl->stage_name = afl->stage_name_buf;
  afl->bytes_trim_in += q->len;

  /* Initialize trimming in the custom mutator */
  afl->stage_cur = 0;
  afl->stage_max = afl->mutator->afl_custom_init_trim(afl, in_buf, q->len);

  if (afl->not_on_tty && afl->debug)
    SAYF("[Custom Trimming] START: Max %d iterations, %u bytes", afl->stage_max,
         q->len);

  while (afl->stage_cur < afl->stage_max) {

    snprintf(afl->stage_name_buf, STAGE_BUF_SIZE, "ptrim %s", DI(trim_exec));

    u32 cksum;

    u8 *   retbuf = NULL;
    size_t retlen = 0;

    afl->mutator->afl_custom_trim(afl, &retbuf, &retlen);

    if (retlen > orig_len)
      FATAL(
          "Trimmed data returned by custom mutator is larger than original "
          "data");

    write_to_testcase(afl, retbuf, retlen);

    fault = run_target(afl, afl->fsrv.exec_tmout);
    ++afl->trim_execs;

    if (afl->stop_soon || fault == FAULT_ERROR) {

      ck_free(retbuf);
      goto abort_trimming;

    }

    cksum = hash32(afl->fsrv.trace_bits, MAP_SIZE, HASH_CONST);

    if (cksum == q->exec_cksum) {

      q->len = retlen;
      memcpy(in_buf, retbuf, retlen);

      /* Let's save a clean trace, which will be needed by
         update_bitmap_score once we're done with the trimming stuff. */

      if (!needs_write) {

        needs_write = 1;
        memcpy(afl->clean_trace_custom, afl->fsrv.trace_bits, MAP_SIZE);

      }

      /* Tell the custom mutator that the trimming was successful */
      afl->stage_cur = afl->mutator->afl_custom_post_trim(afl, 1);

      if (afl->not_on_tty && afl->debug)
        SAYF("[Custom Trimming] SUCCESS: %d/%d iterations (now at %u bytes)",
             afl->stage_cur, afl->stage_max, q->len);

    } else {

      /* Tell the custom mutator that the trimming was unsuccessful */
      afl->stage_cur = afl->mutator->afl_custom_post_trim(afl, 0);
      if (afl->not_on_tty && afl->debug)
        SAYF("[Custom Trimming] FAILURE: %d/%d iterations", afl->stage_cur,
             afl->stage_max);

    }

    ck_free(retbuf);

    /* Since this can be slow, update the screen every now and then. */

    if (!(trim_exec++ % afl->stats_update_freq)) show_stats(afl);

  }

  if (afl->not_on_tty && afl->debug)
    SAYF("[Custom Trimming] DONE: %u bytes -> %u bytes", orig_len, q->len);

  /* If we have made changes to in_buf, we also need to update the on-disk
     version of the test case. */

  if (needs_write) {

    s32 fd;

    unlink(q->fname);                                      /* ignore errors */

    fd = open(q->fname, O_WRONLY | O_CREAT | O_EXCL, 0600);

    if (fd < 0) PFATAL("Unable to create '%s'", q->fname);

    ck_write(fd, in_buf, q->len, q->fname);
    close(fd);

    memcpy(afl->fsrv.trace_bits, afl->clean_trace_custom, MAP_SIZE);
    update_bitmap_score(afl, q);

  }

abort_trimming:

  afl->bytes_trim_out += q->len;
  return fault;

}

#ifdef USE_PYTHON
void load_custom_mutator_py(afl_state_t *afl, const char *module_name) {

  PyObject **py_functions = afl->py_functions;

  afl->mutator = ck_alloc(sizeof(struct custom_mutator));

  afl->mutator->name = module_name;
  ACTF("Loading Python mutator library from '%s'...", module_name);

  if (py_functions[PY_FUNC_INIT]) afl->mutator->afl_custom_init = init_py;

  /* "afl_custom_fuzz" should not be NULL, but the interface of Python mutator
     is quite different from the custom mutator. */
  afl->mutator->afl_custom_fuzz = fuzz_py;

  if (py_functions[PY_FUNC_PRE_SAVE])
    afl->mutator->afl_custom_pre_save = pre_save_py;

  if (py_functions[PY_FUNC_INIT_TRIM])
    afl->mutator->afl_custom_init_trim = init_trim_py;

  if (py_functions[PY_FUNC_POST_TRIM])
    afl->mutator->afl_custom_post_trim = post_trim_py;

  if (py_functions[PY_FUNC_TRIM]) afl->mutator->afl_custom_trim = trim_py;

  if (py_functions[PY_FUNC_HAVOC_MUTATION])
    afl->mutator->afl_custom_havoc_mutation = havoc_mutation_py;

  if (py_functions[PY_FUNC_HAVOC_MUTATION_PROBABILITY])
    afl->mutator->afl_custom_havoc_mutation_probability =
        havoc_mutation_probability_py;

  if (py_functions[PY_FUNC_QUEUE_GET])
    afl->mutator->afl_custom_queue_get = queue_get_py;

  if (py_functions[PY_FUNC_QUEUE_NEW_ENTRY])
    afl->mutator->afl_custom_queue_new_entry = queue_new_entry_py;

  OKF("Python mutator '%s' installed successfully.", module_name);

  /* Initialize the custom mutator */
  if (afl->mutator->afl_custom_init)
    afl->mutator->afl_custom_init(afl, UR(afl, 0xFFFFFFFF));

}

#endif