/src/dns_config_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2023 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include <fcntl.h> |
14 | | #include <stddef.h> |
15 | | #include <stdint.h> |
16 | | #include <stdlib.h> |
17 | | #include <string> |
18 | | #include <sys/socket.h> |
19 | | #include <sys/stat.h> |
20 | | #include <sys/types.h> |
21 | | #include <unistd.h> |
22 | | |
23 | | #include <fuzzer/FuzzedDataProvider.h> |
24 | | |
25 | | extern "C" { |
26 | | #include "libevent/include/event2/buffer.h" |
27 | | #include "libevent/include/event2/bufferevent.h" |
28 | | #include "libevent/include/event2/dns.h" |
29 | | #include "libevent/include/event2/event.h" |
30 | | } |
31 | | |
32 | 4.40k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
33 | | |
34 | 4.40k | FuzzedDataProvider data_provider(data, size); |
35 | | |
36 | 4.40k | uint32_t flags = data_provider.ConsumeIntegral<uint32_t>(); |
37 | 4.40k | std::string s1 = data_provider.ConsumeRandomLengthString(); |
38 | 4.40k | std::string s2 = data_provider.ConsumeRandomLengthString(); |
39 | | |
40 | 4.40k | struct event_base *base = NULL; |
41 | 4.40k | struct evdns_base *dns = NULL; |
42 | | |
43 | 4.40k | base = event_base_new(); |
44 | 4.40k | dns = evdns_base_new(base, flags % 65537); |
45 | | |
46 | | /* Create resolv.conf file*/ |
47 | 4.40k | char resolvFilename[50]; |
48 | 4.40k | sprintf(resolvFilename, "/tmp/resolv.%d", getpid()); |
49 | 4.40k | FILE *fp = fopen(resolvFilename, "wb"); |
50 | 4.40k | if (!fp) { |
51 | 0 | goto cleanup; |
52 | 0 | } |
53 | 4.40k | fwrite(s1.c_str(), s1.size(), 1, fp); |
54 | 4.40k | fclose(fp); |
55 | | |
56 | 4.40k | evdns_base_resolv_conf_parse(dns, flags % 17, resolvFilename); |
57 | | |
58 | | /* Create /etc/hosts file*/ |
59 | 4.40k | char hostsFilename[50]; |
60 | 4.40k | sprintf(hostsFilename, "/tmp/hosts.%d", getpid()); |
61 | 4.40k | fp = fopen(hostsFilename, "wb"); |
62 | 4.40k | if (!fp) { |
63 | 0 | unlink(resolvFilename); |
64 | 0 | goto cleanup; |
65 | 0 | } |
66 | 4.40k | fwrite(s2.c_str(), s2.size(), 1, fp); |
67 | 4.40k | fclose(fp); |
68 | | |
69 | 4.40k | evdns_base_load_hosts(dns, hostsFilename); |
70 | 4.40k | evdns_base_search_ndots_set(dns, flags); |
71 | | |
72 | 4.40k | unlink(resolvFilename); |
73 | 4.40k | unlink(hostsFilename); |
74 | 4.40k | evdns_base_search_clear(dns); |
75 | 4.40k | evdns_base_clear_host_addresses(dns); |
76 | | |
77 | | /*clean up*/ |
78 | 4.40k | cleanup: |
79 | 4.40k | evdns_base_free(dns, 0); |
80 | 4.40k | event_base_free(base); |
81 | 4.40k | return 0; |
82 | 4.40k | } |