• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, APP_SOCKET_NONE, &result,
24                        &event);
25     netdutils::ScopedAddrinfo result_cleanup(result);
26 }
27 
28 }  // namespace
29 
30 // Entry point of fuzzing test.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)31 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
32     [[maybe_unused]] static const bool initialized = DoInit();
33     // Sets delayQueries to let DnsTlsFrontend handle 2 queries at once.
34     // If the Address Family is AF_UNSPEC, the frontend will receive both ipv4 and ipv6 queries.
35     // Without setting delayQueries, the second query's connection between the dns_tls_frontend and
36     // the fuzzing test may be closed and cause SSL_ERROR_SYSCALL. Then, the service will crash
37     // after calling SSL_shutdown.
38     // TODO: Make the test work without seeing delayQueries.
39     dot.setDelayQueries(2);
40     dot.setDelayQueriesTimeout(1000);
41     FuzzedDataProvider fdp(data, size);
42 
43     auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
44     // Chooses private DNS or not.
45     if (fdp.ConsumeBool()) parcel.tlsServers = {};
46     resolverCtrl.setResolverConfiguration(parcel);
47 
48     TestResolvGetaddrinfo(fdp);
49 
50     CleanUp();
51     return 0;
52 }
53 
54 }  // namespace android::net
55