| 1 |
#include <assert.h> |
| 2 |
#include <libdeflate.h> |
| 3 |
#include <stdbool.h> |
| 4 |
#include <stdint.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
#include <sys/mman.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
static void |
| 11 |
alloc_guarded_buffer(size_t size, uint8_t **start_ret, uint8_t **end_ret) |
| 12 |
{ |
| 13 |
const size_t pagesize = sysconf(_SC_PAGESIZE); |
| 14 |
const size_t nr_pages = (size + pagesize - 1) / pagesize; |
| 15 |
uint8_t *base_addr, *start, *end; |
| 16 |
|
| 17 |
/* Allocate buffer and guard pages. */ |
| 18 |
base_addr = mmap(NULL, (nr_pages + 2) * pagesize, PROT_READ|PROT_WRITE, |
| 19 |
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 20 |
assert(base_addr != (uint8_t *)MAP_FAILED); |
| 21 |
start = base_addr + pagesize; |
| 22 |
end = start + (nr_pages * pagesize); |
| 23 |
|
| 24 |
/* Unmap the guard pages. */ |
| 25 |
munmap(base_addr, pagesize); |
| 26 |
munmap(end, pagesize); |
| 27 |
|
| 28 |
*start_ret = start; |
| 29 |
*end_ret = end; |
| 30 |
} |
| 31 |
|
| 32 |
static void |
| 33 |
free_guarded_buffer(uint8_t *start, uint8_t *end) |
| 34 |
{ |
| 35 |
munmap(start, end - start); |
| 36 |
} |
| 37 |
|
| 38 |
/* Fuzz the DEFLATE compression and decompression round trip. */ |
| 39 |
int LLVMFuzzerTestOneInput(const uint8_t *in, size_t insize) |
| 40 |
{ |
| 41 |
int level; |
| 42 |
bool use_bound; |
| 43 |
struct libdeflate_compressor *c; |
| 44 |
struct libdeflate_decompressor *d; |
| 45 |
size_t csize_avail; |
| 46 |
uint8_t *ubuf_start, *ubuf_end, *ubuf; |
| 47 |
uint8_t *cbuf_start, *cbuf_end, *cbuf; |
| 48 |
uint8_t *dbuf_start, *dbuf_end, *dbuf; |
| 49 |
size_t csize; |
| 50 |
enum libdeflate_result res; |
| 51 |
|
| 52 |
if (insize < 2) |
| 53 |
return 0; |
| 54 |
|
| 55 |
level = in[0] % 13; |
| 56 |
use_bound = in[1] % 2; |
| 57 |
in += 2; |
| 58 |
insize -= 2; |
| 59 |
|
| 60 |
c = libdeflate_alloc_compressor(level); |
| 61 |
d = libdeflate_alloc_decompressor(); |
| 62 |
|
| 63 |
/* Use guard pages to make all input/output buffer overflows segfault */ |
| 64 |
|
| 65 |
alloc_guarded_buffer(insize, &ubuf_start, &ubuf_end); |
| 66 |
ubuf = ubuf_end - insize; |
| 67 |
memcpy(ubuf, in, insize); |
| 68 |
|
| 69 |
csize_avail = use_bound ? libdeflate_deflate_compress_bound(c, insize) : |
| 70 |
insize; |
| 71 |
alloc_guarded_buffer(csize_avail, &cbuf_start, &cbuf_end); |
| 72 |
cbuf = cbuf_end - csize_avail; |
| 73 |
|
| 74 |
alloc_guarded_buffer(insize, &dbuf_start, &dbuf_end); |
| 75 |
dbuf = dbuf_end - insize; |
| 76 |
|
| 77 |
csize = libdeflate_deflate_compress(c, ubuf, insize, cbuf, csize_avail); |
| 78 |
if (csize != 0) { |
| 79 |
assert(csize <= csize_avail); |
| 80 |
memmove(cbuf_end - csize, cbuf, csize); |
| 81 |
res = libdeflate_deflate_decompress(d, cbuf_end - csize, csize, |
| 82 |
dbuf, insize, NULL); |
| 83 |
assert(res == LIBDEFLATE_SUCCESS); |
| 84 |
assert(memcmp(in, dbuf, insize) == 0); |
| 85 |
} else { |
| 86 |
assert(!use_bound); |
| 87 |
} |
| 88 |
|
| 89 |
libdeflate_free_compressor(c); |
| 90 |
libdeflate_free_decompressor(d); |
| 91 |
free_guarded_buffer(ubuf_start, ubuf_end); |
| 92 |
free_guarded_buffer(cbuf_start, cbuf_end); |
| 93 |
free_guarded_buffer(dbuf_start, dbuf_end); |
| 94 |
return 0; |
| 95 |
} |