Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/resolver_getaddrinfo_shim.cc
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
/// @file
8
/// @brief Target-local deterministic getaddrinfo backend for resolver fuzzing.
9
10
#include <arpa/inet.h>
11
#include <netdb.h>
12
#include <netinet/in.h>
13
#include <sys/socket.h>
14
15
#include <cerrno>
16
#include <chrono>
17
#include <cstdint>
18
#include <cstdlib>
19
#include <cstring>
20
#include <thread>
21
22
#include "proto_fuzzer/resolver_cancel_gate.h"
23
24
namespace {
25
26
constexpr char kPositiveHost[] = "resolver-positive.test";
27
constexpr char kPartialHost[] = "resolver-partial.test";
28
constexpr char kNegativeHost[] = "resolver-negative.test";
29
constexpr char kLinkLocalHost[] = "resolver-link-local.test";
30
constexpr char kQueuedCancelHost[] = "resolver-queued-cancel.test";
31
32
2.30k
unsigned short ParsePort(const char* service) {
33
2.30k
  if (service == nullptr || *service == '\0') {
34
0
    return 0;
35
0
  }
36
2.30k
  char* end = nullptr;
37
2.30k
  errno = 0;
38
2.30k
  const unsigned long value = std::strtoul(service, &end, 10);
39
2.30k
  if (errno != 0 || end == service || *end != '\0' || value > UINT16_MAX) {
40
0
    return 0;
41
0
  }
42
2.30k
  return static_cast<unsigned short>(value);
43
2.30k
}
44
45
4.41k
addrinfo* AllocateAddress(int family, const addrinfo* hints, unsigned short port, const void* address) {
46
4.41k
  auto* node = static_cast<addrinfo*>(std::calloc(1, sizeof(addrinfo)));
47
4.41k
  if (node == nullptr) {
48
0
    return nullptr;
49
0
  }
50
51
4.41k
  const std::size_t address_size = family == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
52
4.41k
  auto* socket_address = static_cast<sockaddr*>(std::calloc(1, address_size));
53
4.41k
  if (socket_address == nullptr) {
54
0
    std::free(node);
55
0
    return nullptr;
56
0
  }
57
58
4.41k
  node->ai_flags = hints == nullptr ? 0 : hints->ai_flags;
59
4.41k
  node->ai_family = family;
60
4.41k
  node->ai_socktype = hints == nullptr || hints->ai_socktype == 0 ? SOCK_STREAM : hints->ai_socktype;
61
4.41k
  node->ai_protocol = hints == nullptr ? 0 : hints->ai_protocol;
62
4.41k
  node->ai_addrlen = static_cast<socklen_t>(address_size);
63
4.41k
  node->ai_addr = socket_address;
64
65
4.41k
  if (family == AF_INET) {
66
2.42k
    auto* ipv4 = reinterpret_cast<sockaddr_in*>(socket_address);
67
2.42k
    ipv4->sin_family = AF_INET;
68
2.42k
    ipv4->sin_port = htons(port);
69
2.42k
    std::memcpy(&ipv4->sin_addr, address, sizeof(ipv4->sin_addr));
70
2.42k
  } else {
71
1.99k
    auto* ipv6 = reinterpret_cast<sockaddr_in6*>(socket_address);
72
1.99k
    ipv6->sin6_family = AF_INET6;
73
1.99k
    ipv6->sin6_port = htons(port);
74
1.99k
    std::memcpy(&ipv6->sin6_addr, address, sizeof(ipv6->sin6_addr));
75
1.99k
  }
76
4.41k
  return node;
77
4.41k
}
78
79
bool AppendAddress(addrinfo** head, addrinfo** tail, int family, const addrinfo* hints, unsigned short port,
80
4.41k
                   const char* address) {
81
4.41k
  in_addr ipv4{};
82
4.41k
  in6_addr ipv6{};
83
4.41k
  const void* bytes = nullptr;
84
4.41k
  if (family == AF_INET) {
85
2.42k
    if (inet_pton(AF_INET, address, &ipv4) != 1) {
86
0
      return false;
87
0
    }
88
2.42k
    bytes = &ipv4;
89
2.42k
  } else {
90
1.99k
    if (inet_pton(AF_INET6, address, &ipv6) != 1) {
91
0
      return false;
92
0
    }
93
1.99k
    bytes = &ipv6;
94
1.99k
  }
95
96
4.41k
  addrinfo* node = AllocateAddress(family, hints, port, bytes);
97
4.41k
  if (node == nullptr) {
98
0
    return false;
99
0
  }
100
4.41k
  if (*tail == nullptr) {
101
2.30k
    *head = node;
102
2.30k
  } else {
103
2.10k
    (*tail)->ai_next = node;
104
2.10k
  }
105
4.41k
  *tail = node;
106
4.41k
  return true;
107
4.41k
}
108
109
2.30k
void FreeAddressList(addrinfo* address) noexcept {
110
6.72k
  while (address != nullptr) {
111
4.41k
    addrinfo* next = address->ai_next;
112
4.41k
    std::free(address->ai_addr);
113
4.41k
    std::free(address->ai_canonname);
114
4.41k
    std::free(address);
115
4.41k
    address = next;
116
4.41k
  }
117
2.30k
}
118
119
}  // namespace
120
121
/// Replace libc name service only in curl_fuzzer_proto_resolver. The fixed
122
/// hostnames are installed by target policy, so no mutation or ambient
123
/// resolver configuration can select another destination.
124
/// @param nodename Hostname selected by the fixed resolver target policy.
125
/// @param service Decimal service port, or null when no port is requested.
126
/// @param hints Requested address family and socket properties, or null.
127
/// @param[out] result Newly allocated address chain on success.
128
/// @return Zero on success, otherwise the matching EAI_* resolver error.
129
2.34k
extern "C" int getaddrinfo(const char* nodename, const char* service, const addrinfo* hints, addrinfo** result) {
130
2.34k
  if (result == nullptr) {
131
0
    return EAI_FAIL;
132
0
  }
133
2.34k
  *result = nullptr;
134
2.34k
  if (nodename == nullptr) {
135
0
    return EAI_NONAME;
136
0
  }
137
138
2.34k
  const bool negative = std::strcmp(nodename, kNegativeHost) == 0;
139
140
2.34k
  const bool positive = std::strcmp(nodename, kPositiveHost) == 0;
141
2.34k
  const bool partial = std::strcmp(nodename, kPartialHost) == 0;
142
2.34k
  const bool link_local = std::strcmp(nodename, kLinkLocalHost) == 0;
143
2.34k
  const bool queued_cancel = std::strcmp(nodename, kQueuedCancelHost) == 0;
144
2.34k
  if (!positive && !negative && !partial && !link_local && !queued_cancel) {
145
0
    return EAI_NONAME;
146
0
  }
147
148
  // This hostname is selected only by the queued-cancellation resolver mode.
149
  // The main thread waits until the first handle's first family is stopped
150
  // here, then adds and starts a duplicate handle. With the public resolver
151
  // pool capped at one worker, no later family can be dequeued before the
152
  // duplicate's pending resolver work is cancelled.
153
2.34k
  if (queued_cancel) {
154
476
    proto_fuzzer::WaitAtResolverCancelGate();
155
476
  }
156
157
  // Hold every owned answer for one bounded interval so curl's first public
158
  // poll observes live resolver work. Unlike a scheduler race or debug-only
159
  // environment hook, this is part of the target-local backend contract.
160
2.34k
  std::this_thread::sleep_for(std::chrono::milliseconds(1));
161
2.34k
  if (negative) {
162
42
    return EAI_NONAME;
163
42
  }
164
165
2.30k
  const int family = hints == nullptr ? AF_UNSPEC : hints->ai_family;
166
2.30k
  if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6) {
167
0
    return EAI_FAMILY;
168
0
  }
169
170
2.30k
  const unsigned short port = ParsePort(service);
171
2.30k
  if (partial && family == AF_INET) {
172
    // Keep the A answer behind the AAAA answer by a small, fixed interval.
173
    // curl can then consume the first asynchronous family through its live
174
    // resolver accessor before the complete result moves into the DNS cache.
175
553
    std::this_thread::sleep_for(std::chrono::milliseconds(4));
176
553
  }
177
2.30k
  addrinfo* head = nullptr;
178
2.30k
  addrinfo* tail = nullptr;
179
2.30k
  bool ok = true;
180
2.30k
  if (positive || partial || queued_cancel) {
181
1.90k
    if (family == AF_UNSPEC || family == AF_INET6) {
182
796
      ok = AppendAddress(&head, &tail, AF_INET6, hints, port, "::1") &&
183
796
           AppendAddress(&head, &tail, AF_INET6, hints, port, "::2");
184
796
    }
185
1.90k
    if (ok && (family == AF_UNSPEC || family == AF_INET)) {
186
1.11k
      ok = AppendAddress(&head, &tail, AF_INET, hints, port, "127.0.0.1") &&
187
1.11k
           AppendAddress(&head, &tail, AF_INET, hints, port, "127.0.0.2");
188
1.11k
    }
189
1.90k
  } else if (family == AF_INET6) {
190
201
    ok = AppendAddress(&head, &tail, AF_INET6, hints, port, "fe80::1");
191
201
  } else if (family == AF_UNSPEC) {
192
198
    ok = AppendAddress(&head, &tail, AF_INET6, hints, port, "fe80::1") &&
193
198
         AppendAddress(&head, &tail, AF_INET, hints, port, "127.0.0.1");
194
18.4E
  } else {
195
18.4E
    ok = AppendAddress(&head, &tail, AF_INET, hints, port, "127.0.0.1");
196
18.4E
  }
197
198
2.30k
  if (!ok || head == nullptr) {
199
0
    FreeAddressList(head);
200
0
    return EAI_MEMORY;
201
0
  }
202
2.30k
  *result = head;
203
2.30k
  return 0;
204
2.30k
}
205
206
/// Match the ownership contract of the target-local getaddrinfo definition.
207
/// @param address Address chain returned by the target-local getaddrinfo.
208
2.30k
extern "C" void freeaddrinfo(addrinfo* address) noexcept { FreeAddressList(address); }