1 #include <netdb.h>
2 #include <sys/param.h>
3 
4 #include <string>
5 
6 #include "resolv_fuzzer_utils.h"
7 
8 namespace android::net {
9 namespace {
10 
11 // Tests resolv_getaddrinfo.
TestResolvGetaddrinfo(FuzzedDataProvider & fdp)12 void TestResolvGetaddrinfo(FuzzedDataProvider& fdp) {
13     std::string hostname = fdp.ConsumeRandomLengthString(MAXHOSTNAMELEN);
14     std::string servname = fdp.ConsumeRandomLengthString(MAXHOSTNAMELEN);
15     // All valid address families in socket.h, e.g. AF_INET.
16     int af = fdp.ConsumeIntegralInRange<int>(0, AF_MAX);
17     int socktype = RandomSocketType(fdp);
18     addrinfo hints = {.ai_family = af, .ai_socktype = socktype};
19     addrinfo* result;
20     NetworkDnsEventReported event;
21 
22     resolv_getaddrinfo(hostname.c_str(), fdp.ConsumeBool() ? servname.c_str() : nullptr,
23                        fdp.ConsumeBool() ? &hints : nullptr, &mNetContext, &result, &event);
24     netdutils::ScopedAddrinfo result_cleanup(result);
25 }
26 
27 }  // namespace
28 
29 // Entry point of fuzzing test.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)30 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
31     [[maybe_unused]] static const bool initialized = DoInit();
32     // Sets delayQueries to let DnsTlsFrontend handle 2 queries at once.
33     // If the Address Family is AF_UNSPEC, the frontend will receive both ipv4 and ipv6 queries.
34     // Without setting delayQueries, the second query's connection between the dns_tls_frontend and
35     // the fuzzing test may be closed and cause SSL_ERROR_SYSCALL. Then, the service will crash
36     // after calling SSL_shutdown.
37     // TODO: Make the test work without seeing delayQueries.
38     dot.setDelayQueries(2);
39     dot.setDelayQueriesTimeout(1000);
40     FuzzedDataProvider fdp(data, size);
41 
42     auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
43     // Chooses private DNS or not.
44     if (fdp.ConsumeBool()) parcel.tlsServers = {};
45     resolverCtrl.setResolverConfiguration(parcel);
46 
47     TestResolvGetaddrinfo(fdp);
48 
49     CleanUp();
50     return 0;
51 }
52 
53 }  // namespace android::net
54