1 /* MIT License
2 *
3 * Copyright (c) The c-ares project and its contributors
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26 /*
27 * General driver to allow command-line fuzzer (i.e. afl) to
28 * exercise the libFuzzer entrypoint.
29 */
30 #include <stdio.h>
31
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #ifdef _WIN32
37 # include <io.h>
38 #else
39 # include <unistd.h>
40 #endif
41
42 #include "ares.h"
43
44 #define kMaxAflInputSize (1 << 20)
45 static unsigned char afl_buffer[kMaxAflInputSize];
46
47 #ifdef __AFL_LOOP
48 /* If we are built with afl-clang-fast, use persistent mode */
49 # define KEEP_FUZZING(count) __AFL_LOOP(1000)
50 #else
51 /* If we are built with afl-clang, execute each input once */
52 # define KEEP_FUZZING(count) ((count) < 1)
53 #endif
54
55 /* In ares-test-fuzz.c and ares-test-fuzz-name.c: */
56 int LLVMFuzzerTestOneInput(const unsigned char *data, unsigned long size);
57
ProcessFile(int fd)58 static void ProcessFile(int fd)
59 {
60 ares_ssize_t count = read(fd, afl_buffer, kMaxAflInputSize);
61 /*
62 * Make a copy of the data so that it's not part of a larger
63 * buffer (where buffer overflows would go unnoticed).
64 */
65 if (count > 0) {
66 unsigned char *copied_data = (unsigned char *)malloc((size_t)count);
67 memcpy(copied_data, afl_buffer, (size_t)count);
68 LLVMFuzzerTestOneInput(copied_data, (size_t)count);
69 free(copied_data);
70 }
71 }
72
main(int argc,char * argv[])73 int main(int argc, char *argv[])
74 {
75 if (argc == 1) {
76 int count = 0;
77 while (KEEP_FUZZING(count)) {
78 #ifndef STDIN_FILENO
79 ProcessFile(fileno(stdin));
80 #else
81 ProcessFile(STDIN_FILENO);
82 #endif
83 count++;
84 }
85 } else {
86 int ii;
87 for (ii = 1; ii < argc; ++ii) {
88 int fd = open(argv[ii], O_RDONLY);
89 if (fd < 0) {
90 fprintf(stderr, "Failed to open '%s'\n", argv[ii]);
91 continue;
92 }
93 ProcessFile(fd);
94 close(fd);
95 }
96 }
97 return 0;
98 }
99