| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <errno.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <stdint.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#include "fuzz.h" |
| 24 |
|
| 25 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 26 |
|
| 27 |
#include <dirent.h> |
| 28 |
|
| 29 |
static void |
| 30 |
test_one_file(const char *filename) { |
| 31 |
int fd; |
| 32 |
struct stat st; |
| 33 |
char *data; |
| 34 |
ssize_t n; |
| 35 |
|
| 36 |
if ((fd = open(filename, O_RDONLY)) == -1) { |
| 37 |
fprintf(stderr, "Failed to open %s: %s\n", filename, |
| 38 |
strerror(errno)); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if (fstat(fd, &st) != 0) { |
| 43 |
fprintf(stderr, "Failed to stat %s: %s\n", filename, |
| 44 |
strerror(errno)); |
| 45 |
goto closefd; |
| 46 |
} |
| 47 |
|
| 48 |
data = malloc(st.st_size); |
| 49 |
n = read(fd, data, st.st_size); |
| 50 |
if (n == st.st_size) { |
| 51 |
printf("testing %zd bytes from %s\n", n, filename); |
| 52 |
fflush(stdout); |
| 53 |
LLVMFuzzerTestOneInput((const uint8_t *)data, n); |
| 54 |
fflush(stderr); |
| 55 |
} else { |
| 56 |
if (n < 0) { |
| 57 |
fprintf(stderr, |
| 58 |
"Failed to read %zd bytes from %s: %s\n", |
| 59 |
(ssize_t)st.st_size, filename, strerror(errno)); |
| 60 |
} else { |
| 61 |
fprintf(stderr, |
| 62 |
"Failed to read %zd bytes from %s, got %zd\n", |
| 63 |
(ssize_t)st.st_size, filename, n); |
| 64 |
} |
| 65 |
} |
| 66 |
free(data); |
| 67 |
closefd: |
| 68 |
close(fd); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
test_all_from(const char *dirname) { |
| 73 |
DIR *dirp; |
| 74 |
struct dirent *dp; |
| 75 |
|
| 76 |
dirp = opendir(dirname); |
| 77 |
if (dirp == NULL) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
while ((dp = readdir(dirp)) != NULL) { |
| 82 |
char filename[strlen(dirname) + strlen(dp->d_name) + 2]; |
| 83 |
|
| 84 |
if (dp->d_name[0] == '.') { |
| 85 |
continue; |
| 86 |
} |
| 87 |
snprintf(filename, sizeof(filename), "%s/%s", dirname, |
| 88 |
dp->d_name); |
| 89 |
test_one_file(filename); |
| 90 |
} |
| 91 |
|
| 92 |
closedir(dirp); |
| 93 |
} |
| 94 |
|
| 95 |
int |
| 96 |
main(int argc, char **argv) { |
| 97 |
int ret; |
| 98 |
char corpusdir[PATH_MAX]; |
| 99 |
const char *target = strrchr(argv[0], '/'); |
| 100 |
|
| 101 |
ret = LLVMFuzzerInitialize(&argc, &argv); |
| 102 |
if (ret != 0) { |
| 103 |
fprintf(stderr, "LLVMFuzzerInitialize failure: %d\n", ret); |
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
if (argv[1] != NULL && strcmp(argv[1], "-d") == 0) { |
| 108 |
debug = true; |
| 109 |
argv++; |
| 110 |
argc--; |
| 111 |
} |
| 112 |
|
| 113 |
if (argv[1] != NULL) { |
| 114 |
while (argv[1] != NULL) { |
| 115 |
test_one_file(argv[1]); |
| 116 |
argv++; |
| 117 |
argc--; |
| 118 |
} |
| 119 |
POST(argc); |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
target = (target != NULL) ? target + 1 : argv[0]; |
| 124 |
if (strncmp(target, "lt-", 3) == 0) { |
| 125 |
target += 3; |
| 126 |
} |
| 127 |
|
| 128 |
snprintf(corpusdir, sizeof(corpusdir), FUZZDIR "/%s.in", target); |
| 129 |
|
| 130 |
test_all_from(corpusdir); |
| 131 |
|
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
#elif __AFL_COMPILER |
| 136 |
|
| 137 |
int |
| 138 |
main(int argc, char **argv) { |
| 139 |
int ret; |
| 140 |
unsigned char buf[64 * 1024]; |
| 141 |
|
| 142 |
LLVMFuzzerInitialize(&argc, &argv); |
| 143 |
if (ret != 0) { |
| 144 |
fprintf(stderr, "LLVMFuzzerInitialize failure: %d\n", ret); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
#ifdef __AFL_LOOP |
| 149 |
while (__AFL_LOOP(10000)) { /* only works with afl-clang-fast */ |
| 150 |
#else /* ifdef __AFL_LOOP */ |
| 151 |
{ |
| 152 |
#endif /* ifdef __AFL_LOOP */ |
| 153 |
ret = fread(buf, 1, sizeof(buf), stdin); |
| 154 |
if (ret < 0) { |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
LLVMFuzzerTestOneInput(buf, ret); |
| 159 |
} |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |