aboutsummaryrefslogtreecommitdiff
path: root/utils/libdislocator/libdislocator.so.c
blob: b80be1a1fd6827d72cc6865f298fa9783a39a1fe (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*

   american fuzzy lop++ - dislocator, an abusive allocator
   -----------------------------------------------------

   Originally written by Michal Zalewski

   Copyright 2016 Google Inc. All rights reserved.
   Copyright 2019-2024 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 a companion library that can be used as a drop-in replacement
   for the libc allocator in the fuzzed binaries. See README.dislocator.md for
   more info.

 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/mman.h>

#ifdef __APPLE__
  #include <mach/vm_statistics.h>
#endif

#ifdef __FreeBSD__
  #include <sys/param.h>
#endif

#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__HAIKU__)
  #include <unistd.h>
  #include <sys/prctl.h>
  #ifdef __linux__
    #include <sys/syscall.h>
    #include <malloc.h>
  #endif
  #ifdef __NR_getrandom
    #define arc4random_buf(p, l)                       \
      do {                                             \
                                                       \
        ssize_t rd = syscall(__NR_getrandom, p, l, 0); \
        if (rd != l) DEBUGF("getrandom failed");       \
                                                       \
      } while (0)

  #else
    #include <time.h>
    #define arc4random_buf(p, l)     \
      do {                           \
                                     \
        srand(time(NULL));           \
        u32 i;                       \
        u8 *ptr = (u8 *)p;           \
        for (i = 0; i < l; i++)      \
          ptr[i] = rand() % INT_MAX; \
                                     \
      } while (0)

  #endif
  #ifndef PR_SET_VMA
    #define PR_SET_VMA 0x53564d41
    #define PR_SET_VMA_ANON_NAME 0
  #endif
#endif

#include "config.h"
#include "types.h"

#if __STDC_VERSION__ < 201112L || \
    (defined(__FreeBSD__) && __FreeBSD_version < 1200000)
// use this hack if not C11
typedef struct {

  long long   __ll;
  long double __ld;

} max_align_t;

#endif

#define ALLOC_ALIGN_SIZE (_Alignof(max_align_t))

#ifndef PAGE_SIZE
  #define PAGE_SIZE 4096
#endif                                                        /* !PAGE_SIZE */

#ifndef MAP_ANONYMOUS
  #define MAP_ANONYMOUS MAP_ANON
#endif                                                    /* !MAP_ANONYMOUS */

#define SUPER_PAGE_SIZE 1 << 21

/* Error / message handling: */

#define DEBUGF(_x...)                 \
  do {                                \
                                      \
    if (alloc_verbose) {              \
                                      \
      if (++call_depth == 1) {        \
                                      \
        fprintf(stderr, "[AFL] " _x); \
        fprintf(stderr, "\n");        \
                                      \
      }                               \
      call_depth--;                   \
                                      \
    }                                 \
                                      \
  } while (0)

#define FATAL(_x...)                    \
  do {                                  \
                                        \
    if (++call_depth == 1) {            \
                                        \
      fprintf(stderr, "*** [AFL] " _x); \
      fprintf(stderr, " ***\n");        \
      abort();                          \
                                        \
    }                                   \
    call_depth--;                       \
                                        \
  } while (0)

/* Macro to count the number of pages needed to store a buffer: */

#define PG_COUNT(_l) (((_l) + (PAGE_SIZE - 1)) / PAGE_SIZE)

/* Canary & clobber bytes: */

#define ALLOC_CANARY 0xAACCAACC
#define ALLOC_CLOBBER 0xCC

#define TAIL_ALLOC_CANARY 0xAC

#define PTR_C(_p) (((u32 *)(_p))[-1])
#define PTR_L(_p) (((u32 *)(_p))[-2])

/* Configurable stuff (use AFL_LD_* to set): */

static size_t max_mem = MAX_ALLOC;      /* Max heap usage to permit         */
static u8     alloc_verbose,            /* Additional debug messages        */
    hard_fail,                          /* abort() when max_mem exceeded?   */
    no_calloc_over,                     /* abort() on calloc() overflows?   */
    align_allocations;                  /* Force alignment to sizeof(void*) */

#if defined __OpenBSD__ || defined __APPLE__
  #define __thread
  #warning no thread support available
#endif
static _Atomic size_t total_mem;        /* Currently allocated mem          */

static __thread u32 call_depth;         /* To avoid recursion via fprintf() */
static u32          alloc_canary;

/* This is the main alloc function. It allocates one page more than necessary,
   sets that tailing page to PROT_NONE, and then increments the return address
   so that it is right-aligned to that boundary. Since it always uses mmap(),
   the returned memory will be zeroed. */

static void *__dislocator_alloc(size_t len) {

  u8    *ret, *base;
  size_t tlen;
  int    flags, protflags, fd, sp;

  if (total_mem + len > max_mem || total_mem + len < total_mem) {

    if (hard_fail) FATAL("total allocs exceed %zu MB", max_mem / 1024 / 1024);

    DEBUGF("total allocs exceed %zu MB, returning NULL", max_mem / 1024 / 1024);

    return NULL;

  }

  size_t rlen;
  if (align_allocations && (len & (ALLOC_ALIGN_SIZE - 1)))
    rlen = (len & ~(ALLOC_ALIGN_SIZE - 1)) + ALLOC_ALIGN_SIZE;
  else
    rlen = len;

  /* We will also store buffer length and a canary below the actual buffer, so
     let's add 8 bytes for that. */

  base = NULL;
  tlen = (1 + PG_COUNT(rlen + 8)) * PAGE_SIZE;
  protflags = PROT_READ | PROT_WRITE;
  flags = MAP_PRIVATE | MAP_ANONYMOUS;
  fd = -1;
#if defined(PROT_MAX)
  // apply when sysctl vm.imply_prot_max is set to 1
  // no-op otherwise
  protflags |= PROT_MAX(PROT_READ | PROT_WRITE);
#endif
#if defined(USEHUGEPAGE)
  sp = (rlen >= SUPER_PAGE_SIZE && !(rlen % SUPER_PAGE_SIZE));

  #if defined(__APPLE__)
  if (sp) fd = VM_FLAGS_SUPERPAGE_SIZE_2MB;
  #elif defined(__linux__)
  if (sp) flags |= MAP_HUGETLB;
  #elif defined(__FreeBSD__)
  if (sp) flags |= MAP_ALIGNED_SUPER;
  #elif defined(__sun)
  if (sp) {

    base = (void *)(caddr_t)(1 << 21);
    flags |= MAP_ALIGN;

  }

  #endif
#else
  (void)sp;
#endif

  ret = (u8 *)mmap(base, tlen, protflags, flags, fd, 0);
#if defined(USEHUGEPAGE)
  /* We try one more time with regular call */
  if (ret == MAP_FAILED) {

  #if defined(__APPLE__)
    fd = -1;
  #elif defined(__linux__)
    flags &= -MAP_HUGETLB;
  #elif defined(__FreeBSD__)
    flags &= -MAP_ALIGNED_SUPER;
  #elif defined(__sun)
    flags &= -MAP_ALIGN;
  #endif
    ret = (u8 *)mmap(NULL, tlen, protflags, flags, fd, 0);

  }

#endif

  if (ret == MAP_FAILED) {

    if (hard_fail) FATAL("mmap() failed on alloc (OOM?)");

    DEBUGF("mmap() failed on alloc (OOM?)");

    return NULL;

  }

#if defined(USENAMEDPAGE)
  #if defined(__linux__)
  // in the /proc/<pid>/maps file, the anonymous page appears as
  // `<start>-<end> ---p 00000000 00:00 0 [anon:libdislocator]`
  if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)ret, tlen,
            (unsigned long)"libdislocator") < 0) {

    DEBUGF("prctl() failed");

  }

  #endif
#endif

  /* Set PROT_NONE on the last page. */

  if (mprotect(ret + PG_COUNT(rlen + 8) * PAGE_SIZE, PAGE_SIZE, PROT_NONE))
    FATAL("mprotect() failed when allocating memory");

  /* Offset the return pointer so that it's right-aligned to the page
     boundary. */

  ret += PAGE_SIZE * PG_COUNT(rlen + 8) - rlen - 8;

  /* Store allocation metadata. */

  ret += 8;

  PTR_L(ret) = len;
  PTR_C(ret) = alloc_canary;

  total_mem += len;

  if (rlen != len) {

    size_t i;
    for (i = len; i < rlen; ++i)
      ret[i] = TAIL_ALLOC_CANARY;

  }

  return ret;

}

/* The "user-facing" wrapper for calloc(). This just checks for overflows and
   displays debug messages if requested. */

__attribute__((malloc)) __attribute__((alloc_size(1, 2))) void *calloc(
    size_t elem_len, size_t elem_cnt) {

  void *ret;

  size_t len = elem_len * elem_cnt;

  /* Perform some sanity checks to detect obvious issues... */

  if (elem_cnt && len / elem_cnt != elem_len) {

    if (no_calloc_over) {

      DEBUGF("calloc(%zu, %zu) would overflow, returning NULL", elem_len,
             elem_cnt);
      return NULL;

    }

    FATAL("calloc(%zu, %zu) would overflow", elem_len, elem_cnt);

  }

  ret = __dislocator_alloc(len);

  DEBUGF("calloc(%zu, %zu) = %p [%zu total]", elem_len, elem_cnt, ret,
         total_mem);

  return ret;

}

/* The wrapper for malloc(). Roughly the same, also clobbers the returned
   memory (unlike calloc(), malloc() is not guaranteed to return zeroed
   memory). */

__attribute__((malloc)) __attribute__((alloc_size(1))) void *malloc(
    size_t len) {

  void *ret;

  ret = __dislocator_alloc(len);

  DEBUGF("malloc(%zu) = %p [%zu total]", len, ret, total_mem);

  if (ret && len) memset(ret, ALLOC_CLOBBER, len);

  return ret;

}

/* The wrapper for free(). This simply marks the entire region as PROT_NONE.
   If the region is already freed, the code will segfault during the attempt to
   read the canary. Not very graceful, but works, right? */

void free(void *ptr) {

  u32 len;

  DEBUGF("free(%p)", ptr);

  if (!ptr) return;

  if (PTR_C(ptr) != alloc_canary) FATAL("bad allocator canary on free()");

  len = PTR_L(ptr);

  total_mem -= len;
  u8 *ptr_ = ptr;

  if (align_allocations && (len & (ALLOC_ALIGN_SIZE - 1))) {

    size_t rlen = (len & ~(ALLOC_ALIGN_SIZE - 1)) + ALLOC_ALIGN_SIZE;
    for (; len < rlen; ++len)
      if (ptr_[len] != TAIL_ALLOC_CANARY)
        FATAL("bad tail allocator canary on free()");

  }

  /* Protect everything. Note that the extra page at the end is already
     set as PROT_NONE, so we don't need to touch that. */

  ptr_ -= PAGE_SIZE * PG_COUNT(len + 8) - len - 8;

  if (mprotect(ptr_ - 8, PG_COUNT(len + 8) * PAGE_SIZE, PROT_NONE))
    FATAL("mprotect() failed when freeing memory");

  ptr = ptr_;

  /* Keep the mapping; this is wasteful, but prevents ptr reuse. */

}

/* Realloc is pretty straightforward, too. We forcibly reallocate the buffer,
   move data, and then free (aka mprotect()) the original one. */

__attribute__((alloc_size(2))) void *realloc(void *ptr, size_t len) {

  void *ret;

  ret = malloc(len);

  if (ret && ptr) {

    if (PTR_C(ptr) != alloc_canary) FATAL("bad allocator canary on realloc()");
    // Here the tail canary check is delayed to free()

    memcpy(ret, ptr, MIN(len, PTR_L(ptr)));
    free(ptr);

  }

  DEBUGF("realloc(%p, %zu) = %p [%zu total]", ptr, len, ret, total_mem);

  return ret;

}

/* posix_memalign we mainly check the proper alignment argument
   if the requested size fits within the alignment we do
   a normal request */

int posix_memalign(void **ptr, size_t align, size_t len) {

  // if (*ptr == NULL) return EINVAL; // (andrea) Why? I comment it out for now
  if ((align % 2) || (align % sizeof(void *))) return EINVAL;
  if (len == 0) {

    *ptr = NULL;
    return 0;

  }

  size_t rem = len % align;
  if (rem) len += align - rem;

  *ptr = __dislocator_alloc(len);

  if (*ptr && len) memset(*ptr, ALLOC_CLOBBER, len);

  DEBUGF("posix_memalign(%p %zu, %zu) [*ptr = %p]", ptr, align, len, *ptr);

  return 0;

}

/* just the non-posix fashion */

__attribute__((malloc)) __attribute__((alloc_size(2))) void *memalign(
    size_t align, size_t len) {

  void *ret = NULL;

  if (posix_memalign(&ret, align, len)) {

    DEBUGF("memalign(%zu, %zu) failed", align, len);

  }

  return ret;

}

/* sort of C11 alias of memalign only more severe, alignment-wise */

__attribute__((malloc)) __attribute__((alloc_size(2))) void *aligned_alloc(
    size_t align, size_t len) {

  void *ret = NULL;

  if ((len % align)) return NULL;

  if (posix_memalign(&ret, align, len)) {

    DEBUGF("aligned_alloc(%zu, %zu) failed", align, len);

  }

  return ret;

}

/* specific BSD api mainly checking possible overflow for the size */

__attribute__((alloc_size(2, 3))) void *reallocarray(void *ptr, size_t elem_len,
                                                     size_t elem_cnt) {

  const size_t elem_lim = 1UL << (sizeof(size_t) * 4);
  const size_t elem_tot = elem_len * elem_cnt;
  void        *ret = NULL;

  if ((elem_len >= elem_lim || elem_cnt >= elem_lim) && elem_len > 0 &&
      elem_cnt > (SIZE_MAX / elem_len)) {

    DEBUGF("reallocarray size overflow (%zu)", elem_tot);

  } else {

    ret = realloc(ptr, elem_tot);

  }

  return ret;

}

int reallocarr(void *ptr, size_t elem_len, size_t elem_cnt) {

  void        *ret = NULL;
  const size_t elem_tot = elem_len * elem_cnt;

  if (elem_tot == 0) {

    void **h = &ptr;
    *h = ret;
    return 0;

  }

  ret = reallocarray(ptr, elem_len, elem_cnt);
  return ret ? 0 : -1;

}

#if defined(__APPLE__)
size_t malloc_size(const void *ptr) {

#elif !defined(__ANDROID__)
size_t malloc_usable_size(void *ptr) {

#else
size_t malloc_usable_size(const void *ptr) {

#endif

  return ptr ? PTR_L(ptr) : 0;

}

#if defined(__APPLE__)
size_t malloc_good_size(size_t len) {

  return (len & ~(ALLOC_ALIGN_SIZE - 1)) + ALLOC_ALIGN_SIZE;

}

#endif

__attribute__((constructor)) void __dislocator_init(void) {

  char *tmp = getenv("AFL_LD_LIMIT_MB");

  if (tmp) {

    char              *tok;
    unsigned long long mmem = strtoull(tmp, &tok, 10);
    if (*tok != '\0' || errno == ERANGE || mmem > SIZE_MAX / 1024 / 1024)
      FATAL("Bad value for AFL_LD_LIMIT_MB");
    max_mem = mmem * 1024 * 1024;

  }

  alloc_canary = ALLOC_CANARY;
  tmp = getenv("AFL_RANDOM_ALLOC_CANARY");

  if (tmp) arc4random_buf(&alloc_canary, sizeof(alloc_canary));

  alloc_verbose = !!getenv("AFL_LD_VERBOSE");
  hard_fail = !!getenv("AFL_LD_HARD_FAIL");
  no_calloc_over = !!getenv("AFL_LD_NO_CALLOC_OVER");
  align_allocations = !!getenv("AFL_ALIGNED_ALLOC");

}

/* NetBSD fault handler specific api subset */

void (*esetfunc(void (*fn)(int, const char *, ...)))(int, const char *, ...) {

  /* Might not be meaningful to implement; upper calls already report errors */
  return NULL;

}

void *emalloc(size_t len) {

  return malloc(len);

}

void *ecalloc(size_t elem_len, size_t elem_cnt) {

  return calloc(elem_len, elem_cnt);

}

void *erealloc(void *ptr, size_t len) {

  return realloc(ptr, len);

}