aboutsummaryrefslogtreecommitdiff
path: root/custom_mutators/symqemu/symqemu.c
blob: 73a1640a56b83b77580844aaa15babf48ccf160f (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "config.h"
#include "debug.h"
#include "afl-fuzz.h"
#include "common.h"

afl_state_t *afl_struct;
static u32   debug = 0;
static u32   found_items = 0;

#define SYMQEMU_LOCATION "symqemu"

#define DBG(x...) \
  if (debug) { fprintf(stderr, x); }

typedef struct my_mutator {

  afl_state_t *afl;
  u32          all;
  u32          late;
  u8          *mutator_buf;
  u8          *out_dir;
  u8          *target;
  u8          *symqemu;
  u8          *input_file;
  u32          counter;
  u32          seed;
  u32          argc;
  u8         **argv;

} my_mutator_t;

my_mutator_t *afl_custom_init(afl_state_t *afl, unsigned int seed) {

  if (getenv("AFL_DEBUG")) debug = 1;

  my_mutator_t *data = calloc(1, sizeof(my_mutator_t));
  if (!data) {

    perror("afl_custom_init alloc");
    return NULL;

  }

  char *path = getenv("PATH");
  char *exec_name = "symqemu-x86_64";
  char *token = strtok(path, ":");
  char  exec_path[4096];

  while (token != NULL && data->symqemu == NULL) {

    snprintf(exec_path, sizeof(exec_path), "%s/%s", token, exec_name);
    if (access(exec_path, X_OK) == 0) {

      data->symqemu = (u8 *)strdup(exec_path);
      break;

    }

    token = strtok(NULL, ":");

  }

  if (!data->symqemu) FATAL("symqemu binary %s not found", exec_name);
  DBG("Found %s\n", data->symqemu);

  if (getenv("AFL_CUSTOM_MUTATOR_ONLY")) {

    WARNF(
        "the symqemu module is not very effective with "
        "AFL_CUSTOM_MUTATOR_ONLY.");

  }

  if ((data->mutator_buf = malloc(MAX_FILE)) == NULL) {

    free(data);
    perror("mutator_buf alloc");
    return NULL;

  }

  data->target = getenv("AFL_CUSTOM_INFO_PROGRAM");

  u8 *path_tmp = getenv("AFL_CUSTOM_INFO_OUT");
  u32 len = strlen(path_tmp) + 32;
  u8 *symqemu_path = malloc(len);
  data->out_dir = malloc(len);
  snprintf(symqemu_path, len, "%s/%s", path_tmp, SYMQEMU_LOCATION);
  snprintf(data->out_dir, len, "%s/out", symqemu_path, path_tmp);

  (void)mkdir(symqemu_path, 0755);
  (void)mkdir(data->out_dir, 0755);

  setenv("SYMCC_OUTPUT_DIR", data->out_dir, 1);

  data->input_file = getenv("AFL_CUSTOM_INFO_PROGRAM_INPUT");

  u8 *tmp = NULL;
  if ((tmp = getenv("AFL_CUSTOM_INFO_PROGRAM_ARGV")) && *tmp) {

    int argc = 0, index = 2;
    for (u32 i = 0; i < strlen(tmp); ++i)
      if (isspace(tmp[i])) ++argc;

    data->argv = (u8 **)malloc((argc + 4) * sizeof(u8 **));
    u8 *p = strdup(tmp);

    do {

      data->argv[index] = p;
      while (*p && !isspace(*p))
        ++p;
      if (*p) {

        *p++ = 0;
        while (isspace(*p))
          ++p;

      }

      if (strcmp(data->argv[index], "@@") == 0) {

        if (!data->input_file) {

          u32 ilen = strlen(symqemu_path) + 32;
          data->input_file = malloc(ilen);
          snprintf(data->input_file, ilen, "%s/.input", symqemu_path);

        }

        data->argv[index] = data->input_file;

      }

      DBG("%d: %s\n", index, data->argv[index]);
      index++;

    } while (*p);

    data->argv[index] = NULL;
    data->argc = index;

  } else {

    data->argv = (u8 **)malloc(8 * sizeof(u8 **));
    data->argc = 2;
    data->argv[2] = NULL;

  }

  data->argv[0] = data->symqemu;
  data->argv[1] = data->target;
  data->afl = afl;
  data->seed = seed;
  afl_struct = afl;

  if (getenv("SYMQEMU_ALL")) { data->all = 1; }
  if (getenv("SYMQEMU_LATE")) { data->late = 1; }
  if (data->input_file) { setenv("SYMCC_INPUT_FILE", data->input_file, 1); }

  DBG("out_dir=%s, target=%s, input_file=%s, argc=%u\n", data->out_dir,
      data->target,
      data->input_file ? (char *)data->input_file : (char *)"<stdin>",
      data->argc);

  if (debug) {

    fprintf(stderr, "[");
    for (u32 i = 0; i <= data->argc; ++i)
      fprintf(stderr, " \"%s\"",
              data->argv[i] ? (char *)data->argv[i] : "<NULL>");
    fprintf(stderr, " ]\n");

  }

  return data;

}

/* No need to receive a splicing item */
void afl_custom_splice_optout(void *data) {

  (void)(data);

}

/* Get unix time in milliseconds */

inline u64 get_cur_time(void) {

  struct timeval  tv;
  struct timezone tz;

  gettimeofday(&tv, &tz);

  return (tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000);

}

u32 afl_custom_fuzz_count(my_mutator_t *data, const u8 *buf, size_t buf_size) {

  if (likely((!afl_struct->queue_cur->favored && !data->all) ||
             afl_struct->queue_cur->was_fuzzed)) {

    return 0;

  }

  if (likely(data->late)) {

    if (unlikely(get_cur_time() - afl_struct->last_find_time <=
                 10 * 60 * 1000)) {

      return 0;

    }

  }

  int         pipefd[2];
  struct stat st;

  if (afl_struct->afl_env.afl_no_ui) {

    ACTF("Sending to symqemu: %s", afl_struct->queue_cur->fname);

  }

  if (!(stat(afl_struct->queue_cur->fname, &st) == 0 && S_ISREG(st.st_mode) &&
        st.st_size)) {

    PFATAL("Couldn't find enqueued file: %s", afl_struct->queue_cur->fname);

  }

  if (afl_struct->fsrv.use_stdin) {

    if (pipe(pipefd) == -1) {

      PFATAL(
          "Couldn't create a pipe for interacting with symqemu child process");

    }

  }

  if (data->input_file) {

    int     fd = open(data->input_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    ssize_t s = write(fd, buf, buf_size);
    close(fd);
    DBG("wrote %zd/%zd to %s\n", s, buf_size, data->input_file);

  }

  int pid = fork();

  if (pid == -1) return 0;

  if (likely(pid)) {

    if (!data->input_file || afl_struct->fsrv.use_stdin) {

      close(pipefd[0]);

      if (fcntl(pipefd[1], F_GETPIPE_SZ)) {

        fcntl(pipefd[1], F_SETPIPE_SZ, MAX_FILE);

      }

      ck_write(pipefd[1], buf, buf_size, data->input_file);

      close(pipefd[1]);

    }

    pid = waitpid(pid, NULL, 0);
    DBG("symqemu finished executing!\n");

  } else /* (pid == 0) */ {  // child

    if (afl_struct->fsrv.use_stdin) {

      close(pipefd[1]);
      dup2(pipefd[0], 0);

    }

    DBG("exec=%s\n", data->target);
    if (!debug) {

      close(1);
      close(2);
      dup2(afl_struct->fsrv.dev_null_fd, 1);
      dup2(afl_struct->fsrv.dev_null_fd, 2);

    }

    execvp((char *)data->argv[0], (char **)data->argv);
    fprintf(stderr, "Executing: [");
    for (u32 i = 0; i <= data->argc; ++i)
      fprintf(stderr, " \"%s\"",
              data->argv[i] ? (char *)data->argv[i] : "<NULL>");
    fprintf(stderr, " ]\n");
    FATAL("Failed to execute %s %s\n", data->argv[0], data->argv[1]);
    exit(-1);

  }

  /* back in mother process */

  struct dirent **nl;
  s32             i, items = scandir(data->out_dir, &nl, NULL, NULL);
  found_items = 0;
  char source_name[4096];

  if (items > 0) {

    for (i = 0; i < (u32)items; ++i) {

      // symqemu output files start with a digit
      if (!isdigit(nl[i]->d_name[0])) continue;

      struct stat st;
      snprintf(source_name, sizeof(source_name), "%s/%s", data->out_dir,
               nl[i]->d_name);
      DBG("file=%s\n", source_name);

      if (stat(source_name, &st) == 0 && S_ISREG(st.st_mode) && st.st_size) {

        ++found_items;

      }

      free(nl[i]);

    }

    free(nl);

  }

  DBG("Done, found %u items!\n", found_items);

  return found_items;

}

size_t afl_custom_fuzz(my_mutator_t *data, u8 *buf, size_t buf_size,
                       u8 **out_buf, u8 *add_buf, size_t add_buf_size,
                       size_t max_size) {

  struct dirent **nl;
  s32             done = 0, i, items = scandir(data->out_dir, &nl, NULL, NULL);
  char            source_name[4096];

  if (items > 0) {

    for (i = 0; i < (u32)items; ++i) {

      // symqemu output files start with a digit
      if (!isdigit(nl[i]->d_name[0])) continue;

      struct stat st;
      snprintf(source_name, sizeof(source_name), "%s/%s", data->out_dir,
               nl[i]->d_name);
      DBG("file=%s\n", source_name);

      if (stat(source_name, &st) == 0 && S_ISREG(st.st_mode) && st.st_size) {

        int fd = open(source_name, O_RDONLY);
        if (fd < 0) { goto got_an_issue; }

        ssize_t r = read(fd, data->mutator_buf, MAX_FILE);
        close(fd);

        DBG("fn=%s, fd=%d, size=%ld\n", source_name, fd, r);

        if (r < 1) { goto got_an_issue; }

        done = 1;
        --found_items;
        unlink(source_name);

        *out_buf = data->mutator_buf;
        return (u32)r;

      }

      free(nl[i]);

    }

    free(nl);

  }

got_an_issue:
  *out_buf = NULL;
  return 0;

}

/**
 * Deinitialize everything
 *
 * @param data The data ptr from afl_custom_init
 */
void afl_custom_deinit(my_mutator_t *data) {

  free(data->mutator_buf);
  free(data);

}