/src/curl_fuzzer/proto_fuzzer/option_apply.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 Implementation of the option-translation helpers declared in |
9 | | /// option_apply.h. |
10 | | |
11 | | #include "proto_fuzzer/option_apply.h" |
12 | | |
13 | | #include <algorithm> |
14 | | #include <cstddef> |
15 | | #include <cstdint> |
16 | | #include <cstdio> |
17 | | #include <cstdlib> |
18 | | #include <string> |
19 | | #include <string_view> |
20 | | |
21 | | #include "proto_fuzzer/allocation_fault_injection.h" |
22 | | #include "proto_fuzzer/scenario_limits.h" |
23 | | #include "proto_fuzzer/tls_test_credentials.h" |
24 | | |
25 | | namespace proto_fuzzer { |
26 | | |
27 | | namespace { |
28 | | |
29 | | constexpr char kEventDrivenProtocolsAllowed[] = "http,https,ws,wss"; |
30 | | constexpr char kTelnetProtocolAllowed[] = "telnet"; |
31 | | constexpr char kFtpProtocolAllowed[] = "ftp,ftps"; |
32 | | constexpr char kTftpProtocolAllowed[] = "tftp"; |
33 | | constexpr char kSmtpProtocolAllowed[] = "smtp,smtps"; |
34 | | constexpr char kImapProtocolAllowed[] = "imap,imaps"; |
35 | | constexpr char kPop3ProtocolAllowed[] = "pop3,pop3s"; |
36 | | constexpr char kMqttProtocolAllowed[] = "mqtt,mqtts"; |
37 | | constexpr char kDictProtocolAllowed[] = "dict"; |
38 | | constexpr char kFileProtocolAllowed[] = "file"; |
39 | | constexpr char kLdapProtocolAllowed[] = "ldap,ldaps"; |
40 | | constexpr char kRtspProtocolAllowed[] = "rtsp"; |
41 | | constexpr char kConnectToOverride[] = "::127.0.1.127:"; |
42 | | constexpr char kDevNull[] = "/dev/null"; |
43 | | constexpr char kDefaultProvider[] = "default"; |
44 | | constexpr char kEngineCertificateType[] = "ENG"; |
45 | | constexpr char kPemCertificateType[] = "PEM"; |
46 | | constexpr char kPkcs12CertificateType[] = "P12"; |
47 | | constexpr char kProviderCertificateType[] = "PROV"; |
48 | | constexpr unsigned char kMalformedPkcs12[] = {0x30, 0x00}; |
49 | | constexpr char kVerboseEnvVar[] = "FUZZ_VERBOSE"; |
50 | | constexpr char kAltSvcHttpEnvVar[] = "CURL_ALTSVC_HTTP"; |
51 | | constexpr char kHstsHttpEnvVar[] = "CURL_HSTS_HTTP"; |
52 | | constexpr long kConnectTimeoutMs = 200; |
53 | | constexpr long kTimeoutMs = 200; |
54 | | |
55 | | /// Baseline write callback for both CURLOPT_WRITEFUNCTION and |
56 | | /// CURLOPT_HEADERFUNCTION. Consumes every byte so transfers don't stall on |
57 | | /// backpressure and emits nothing. Protocol-specific mocks may install their |
58 | | /// own WRITEFUNCTION afterwards if they need to poke protocol APIs while |
59 | | /// inside a curl callback. |
60 | 8.56M | size_t SilentWriteCallback(void* /*contents*/, size_t size, size_t nmemb, void* /*userdata*/) { return size * nmemb; } |
61 | | |
62 | | /// Consume libcurl's verbose records without emitting per-input diagnostics. |
63 | | /// TELNET's debug build keeps its negotiation/suboption formatters behind the |
64 | | /// verbose switch, so the protocol lane uses this sink to make that reachable |
65 | | /// code fuzzable without turning millions of iterations into log traffic. |
66 | 23.0M | int SilentDebugCallback(CURL* /*handle*/, curl_infotype /*type*/, char* /*data*/, size_t /*size*/, void* /*userdata*/) { |
67 | 23.0M | return 0; |
68 | 23.0M | } |
69 | | |
70 | | /// Let curl's debug build accept transport-security response headers over the |
71 | | /// plaintext HTTP mock. The HTTPS lane now provides a verified peer, but making |
72 | | /// HSTS and Alt-Svc parser coverage depend on a cryptographic handshake would |
73 | | /// needlessly remove those parsers from the high-throughput HTTP lane. |
74 | 492k | void EnableDebugHttpTransportMetadata() { |
75 | 492k | static const bool configured = [] { |
76 | | // CMake builds the fuzzing copy of curl with ENABLE_DEBUG specifically so |
77 | | // these curl-provided test hooks are available. Do not overwrite values a |
78 | | // reproducer deliberately supplied in its environment. |
79 | 30 | (void)setenv(kAltSvcHttpEnvVar, "1", 0); |
80 | 30 | (void)setenv(kHstsHttpEnvVar, "1", 0); |
81 | 30 | return true; |
82 | 30 | }(); |
83 | 492k | (void)configured; |
84 | 492k | } |
85 | | |
86 | 46.3k | void EnableTraceIds() { (void)curl_global_trace("+LIB-IDS"); } |
87 | | |
88 | | } // namespace |
89 | | |
90 | | /// Apply deterministic routing, timeout, output, and persistence defaults. |
91 | | /// Call before applying scenario options. `scheme` identifies the dedicated |
92 | | /// in-process mock that will service the transfer and selects the safe direct- |
93 | | /// protocol allowlist. |
94 | | /// @param easy The curl easy handle to configure. |
95 | | /// @param scheme Protocol whose dedicated in-process mock will service it. |
96 | | /// @param trace_ids Enable verbose LIB-IDS tracing for this scenario. |
97 | | /// @param trace_config Optional curl_global_trace() configuration. |
98 | | /// @return Owner of the CURLOPT_CONNECT_TO list, which must outlive easy cleanup. |
99 | | CurlSlistPtr ApplyBaselineOptions(CURL* easy, curl::fuzzer::proto::Scheme scheme, bool trace_ids, |
100 | 492k | const std::string& trace_config) { |
101 | 492k | EnableDebugHttpTransportMetadata(); |
102 | | |
103 | 492k | curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, &SilentWriteCallback); |
104 | 492k | curl_easy_setopt(easy, CURLOPT_HEADERFUNCTION, &SilentWriteCallback); |
105 | | |
106 | 492k | const bool user_requested_verbose = std::getenv(kVerboseEnvVar) != nullptr; |
107 | 492k | if (!trace_config.empty()) { |
108 | | // Apply the fuzzer-selected categories before any explicit trace-ID |
109 | | // request so a scenario cannot accidentally disable its own ID prefixes. |
110 | 76.6k | (void)curl_global_trace(trace_config.c_str()); |
111 | 76.6k | } |
112 | 492k | if (trace_ids) { |
113 | 46.3k | EnableTraceIds(); |
114 | 46.3k | } |
115 | 492k | if (trace_ids || !trace_config.empty()) { |
116 | | // Keep trace coverage enabled during fuzzing without flooding the process |
117 | | // log. The callback still executes curl_trc.c's formatting path. A stable |
118 | | // ignored userdata pointer also lets internal DoH handles exercise the |
119 | | // public debug-data propagation path without adding per-input ownership. |
120 | 111k | curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, &SilentDebugCallback); |
121 | 111k | curl_easy_setopt(easy, CURLOPT_DEBUGDATA, easy); |
122 | 111k | curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L); |
123 | 381k | } else if (scheme == curl::fuzzer::proto::SCHEME_TELNET && !user_requested_verbose) { |
124 | | // printoption() and printsub() contain a substantial part of curl's TELNET |
125 | | // parser diagnostics but run only in verbose mode. Keep those paths in the |
126 | | // ordinary TELNET coverage lane while suppressing their high-volume text. |
127 | | // An explicit FUZZ_VERBOSE still skips the sink so reproductions remain |
128 | | // inspectable from the terminal. |
129 | 5.34k | curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, &SilentDebugCallback); |
130 | 5.34k | curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L); |
131 | 5.34k | } |
132 | | |
133 | | // Each non-HTTP protocol must use its dedicated peer invariants. Keep them |
134 | | // out of the redirect allowlist so an HTTP response cannot switch a stream |
135 | | // mock into synchronous TELNET, two-channel FTP, or datagram TFTP semantics. |
136 | | // CURLOPT_PROTOCOLS_STR arrived in 7.85.0. |
137 | 492k | const char* direct_protocols = kEventDrivenProtocolsAllowed; |
138 | 492k | switch (scheme) { |
139 | 5.90k | case curl::fuzzer::proto::SCHEME_TELNET: |
140 | 5.90k | direct_protocols = kTelnetProtocolAllowed; |
141 | 5.90k | break; |
142 | 8.06k | case curl::fuzzer::proto::SCHEME_FTP: |
143 | 9.96k | case curl::fuzzer::proto::SCHEME_FTPS: |
144 | 9.96k | direct_protocols = kFtpProtocolAllowed; |
145 | 9.96k | break; |
146 | 2.61k | case curl::fuzzer::proto::SCHEME_TFTP: |
147 | 2.61k | direct_protocols = kTftpProtocolAllowed; |
148 | 2.61k | break; |
149 | 2.74k | case curl::fuzzer::proto::SCHEME_SMTP: |
150 | 6.26k | case curl::fuzzer::proto::SCHEME_SMTPS: |
151 | 6.26k | direct_protocols = kSmtpProtocolAllowed; |
152 | 6.26k | break; |
153 | 2.66k | case curl::fuzzer::proto::SCHEME_IMAP: |
154 | 6.54k | case curl::fuzzer::proto::SCHEME_IMAPS: |
155 | 6.54k | direct_protocols = kImapProtocolAllowed; |
156 | 6.54k | break; |
157 | 1.55k | case curl::fuzzer::proto::SCHEME_POP3: |
158 | 4.25k | case curl::fuzzer::proto::SCHEME_POP3S: |
159 | 4.25k | direct_protocols = kPop3ProtocolAllowed; |
160 | 4.25k | break; |
161 | 1.33k | case curl::fuzzer::proto::SCHEME_MQTT: |
162 | 3.89k | case curl::fuzzer::proto::SCHEME_MQTTS: |
163 | 3.89k | direct_protocols = kMqttProtocolAllowed; |
164 | 3.89k | break; |
165 | 2.94k | case curl::fuzzer::proto::SCHEME_DICT: |
166 | 2.94k | direct_protocols = kDictProtocolAllowed; |
167 | 2.94k | break; |
168 | 5.44k | case curl::fuzzer::proto::SCHEME_GOPHER: |
169 | 5.44k | direct_protocols = "gopher"; |
170 | 5.44k | break; |
171 | 4.50k | case curl::fuzzer::proto::SCHEME_GOPHERS: |
172 | 4.50k | direct_protocols = "gophers"; |
173 | 4.50k | break; |
174 | 3.99k | case curl::fuzzer::proto::SCHEME_FILE: |
175 | 3.99k | direct_protocols = kFileProtocolAllowed; |
176 | 3.99k | break; |
177 | 819 | case curl::fuzzer::proto::SCHEME_LDAP: |
178 | 2.00k | case curl::fuzzer::proto::SCHEME_LDAPS: |
179 | 2.00k | direct_protocols = kLdapProtocolAllowed; |
180 | 2.00k | break; |
181 | 9.04k | case curl::fuzzer::proto::SCHEME_RTSP: |
182 | 9.04k | direct_protocols = kRtspProtocolAllowed; |
183 | 9.04k | break; |
184 | 264k | case curl::fuzzer::proto::SCHEME_HTTP: |
185 | 379k | case curl::fuzzer::proto::SCHEME_HTTPS: |
186 | 399k | case curl::fuzzer::proto::SCHEME_WS: |
187 | 411k | case curl::fuzzer::proto::SCHEME_WSS: |
188 | 425k | case curl::fuzzer::proto::SCHEME_IPFS: |
189 | 425k | case curl::fuzzer::proto::SCHEME_IPNS: |
190 | 425k | case curl::fuzzer::proto::SCHEME_UNSPECIFIED: |
191 | 425k | default: |
192 | 425k | break; |
193 | 492k | } |
194 | 492k | curl_easy_setopt(easy, CURLOPT_PROTOCOLS_STR, direct_protocols); |
195 | 492k | curl_easy_setopt(easy, CURLOPT_REDIR_PROTOCOLS_STR, kEventDrivenProtocolsAllowed); |
196 | | |
197 | | // CONNECT_TO confines direct connections, but an ambient http_proxy or |
198 | | // ALL_PROXY can select a proxy before curl asks the harness for a socket. |
199 | | // An explicit empty proxy keeps every transfer inside the socketpair and |
200 | | // makes replay independent of the machine running the fuzzer. |
201 | 492k | curl_easy_setopt(easy, CURLOPT_PROXY, ""); |
202 | | |
203 | | // Keep raw secure-scheme inputs independent of the host trust store. The |
204 | | // dedicated TLS mock installs its own in-memory trust anchor after this |
205 | | // baseline; scenario options still run last and can deliberately select |
206 | | // verification failures. |
207 | 492k | curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L); |
208 | | |
209 | | // Force every name lookup to the fuzzer's in-process mock peer. The returned |
210 | | // owner keeps the list alive through curl_easy_cleanup. |
211 | 492k | CurlSlistPtr connect_to(curl_slist_append(nullptr, kConnectToOverride)); |
212 | 492k | curl_easy_setopt(easy, CURLOPT_CONNECT_TO, connect_to.get()); |
213 | | |
214 | | // Short bounds: fuzzing should never sit waiting on real I/O. Response |
215 | | // volume is already bounded by libFuzzer's input-size limit, so do not rate |
216 | | // limit receive traffic here: a global bytes-per-second throttle turns every |
217 | | // otherwise-complete large response into wall-clock sleep. |
218 | 492k | curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, kConnectTimeoutMs); |
219 | 492k | curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, kTimeoutMs); |
220 | | |
221 | | // Keep every persistence/read path deterministic and prevent scenarios from |
222 | | // leaking state onto the filesystem. COOKIEFILE also makes the in-memory |
223 | | // engine's RELOAD command traverse its loader against a harmless empty |
224 | | // source. These path options deliberately remain absent from the generated |
225 | | // mutation manifest, so a proto cannot replace /dev/null. |
226 | 492k | curl_easy_setopt(easy, CURLOPT_COOKIEJAR, kDevNull); |
227 | 492k | curl_easy_setopt(easy, CURLOPT_COOKIEFILE, kDevNull); |
228 | 492k | curl_easy_setopt(easy, CURLOPT_ALTSVC, kDevNull); |
229 | 492k | curl_easy_setopt(easy, CURLOPT_HSTS, kDevNull); |
230 | 492k | curl_easy_setopt(easy, CURLOPT_NETRC_FILE, kDevNull); |
231 | | // Do not set CRLFILE to a placeholder path. An empty CRL is |
232 | | // not a harmless sink: when a scenario restores certificate verification, |
233 | | // OpenSSL rejects /dev/null before it can exercise useful handshake and |
234 | | // verification paths. The option is absent from the mutation manifest, so |
235 | | // leaving it unset introduces neither filesystem writes nor external input. |
236 | | |
237 | | // FUZZ_VERBOSE in the environment flips curl's own verbose logging on. This |
238 | | // is useful when reproducing a crashing corpus entry. |
239 | 492k | if (user_requested_verbose) { |
240 | 0 | curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L); |
241 | 0 | } |
242 | 492k | return connect_to; |
243 | 492k | } |
244 | | |
245 | | /// Report whether the active TLS backend needs filename-backed credentials. |
246 | | /// @return True for a backend that does not implement certificate blobs. |
247 | 13.9k | bool TlsClientCredentialFilesRequired() { |
248 | 13.9k | const curl_version_info_data* version = curl_version_info(CURLVERSION_NOW); |
249 | 13.9k | return version != nullptr && version->ssl_version != nullptr && |
250 | 13.9k | std::string_view(version->ssl_version).rfind("GnuTLS/", 0) == 0; |
251 | 13.9k | } |
252 | | |
253 | | /// Apply one closed, harness-owned TLS client-credential configuration. |
254 | | /// No protobuf bytes become a path, provider, or engine identifier. |
255 | | /// @param easy Handle receiving the fixed credential options. |
256 | | /// @param profile Safe credential setup selected by the HTTPS lane. |
257 | | /// @param certificate_path Session-owned certificate path for file-only backends. |
258 | | /// @param private_key_path Session-owned private-key path for file-only backends. |
259 | | /// @return The first curl_easy_setopt error, or CURLE_OK. |
260 | | CURLcode ApplyTlsClientCredentialProfile(CURL* easy, curl::fuzzer::proto::TlsClientCredentialProfile profile, |
261 | 87.2k | const char* certificate_path, const char* private_key_path) { |
262 | 87.2k | if (easy == nullptr) { |
263 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
264 | 0 | } |
265 | | |
266 | 87.2k | const auto set_string = [easy](CURLoption option, const char* value) { |
267 | 25.5k | return curl_easy_setopt(easy, option, value); |
268 | 25.5k | }; |
269 | 87.2k | switch (profile) { |
270 | 73.2k | case curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_NONE: |
271 | 73.2k | return CURLE_OK; |
272 | | |
273 | 3.27k | case curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_MALFORMED_PKCS12: { |
274 | 3.27k | CURLcode result = set_string(CURLOPT_SSLCERTTYPE, kPkcs12CertificateType); |
275 | 3.27k | if (result != CURLE_OK) { |
276 | 15 | return result; |
277 | 15 | } |
278 | 3.25k | const curl_blob blob = {const_cast<unsigned char*>(kMalformedPkcs12), sizeof(kMalformedPkcs12), CURL_BLOB_COPY}; |
279 | 3.25k | return curl_easy_setopt(easy, CURLOPT_SSLCERT_BLOB, &blob); |
280 | 3.27k | } |
281 | | |
282 | 435 | case curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_ENGINE_DEV_NULL: { |
283 | 435 | CURLcode result = set_string(CURLOPT_SSLCERTTYPE, kEngineCertificateType); |
284 | 435 | if (result != CURLE_OK) { |
285 | 4 | return result; |
286 | 4 | } |
287 | 431 | return set_string(CURLOPT_SSLCERT, kDevNull); |
288 | 435 | } |
289 | | |
290 | 2.67k | case curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_DEFAULT_PROVIDER_DEV_NULL: { |
291 | 2.67k | CURLcode result = set_string(CURLOPT_SSLENGINE, kDefaultProvider); |
292 | 2.67k | if (result != CURLE_OK) { |
293 | 2.32k | return result; |
294 | 2.32k | } |
295 | 343 | result = set_string(CURLOPT_SSLCERTTYPE, kProviderCertificateType); |
296 | 343 | if (result != CURLE_OK) { |
297 | 66 | return result; |
298 | 66 | } |
299 | 277 | return set_string(CURLOPT_SSLCERT, kDevNull); |
300 | 343 | } |
301 | | |
302 | 558 | case curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_UNLOADED_PROVIDER_DEV_NULL: { |
303 | 558 | CURLcode result = set_string(CURLOPT_SSLCERTTYPE, kProviderCertificateType); |
304 | 558 | if (result != CURLE_OK) { |
305 | 6 | return result; |
306 | 6 | } |
307 | 552 | return set_string(CURLOPT_SSLCERT, kDevNull); |
308 | 558 | } |
309 | | |
310 | 7.01k | case curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_VALID_PEM: { |
311 | 7.01k | CURLcode result = set_string(CURLOPT_SSLCERTTYPE, kPemCertificateType); |
312 | 7.01k | if (result != CURLE_OK) { |
313 | 28 | return result; |
314 | 28 | } |
315 | 6.98k | result = set_string(CURLOPT_SSLKEYTYPE, kPemCertificateType); |
316 | 6.98k | if (result != CURLE_OK) { |
317 | 60 | return result; |
318 | 60 | } |
319 | 6.92k | if (TlsClientCredentialFilesRequired()) { |
320 | 1.52k | if (certificate_path == nullptr || private_key_path == nullptr) { |
321 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
322 | 0 | } |
323 | 1.52k | result = set_string(CURLOPT_SSLCERT, certificate_path); |
324 | 1.52k | return result == CURLE_OK ? set_string(CURLOPT_SSLKEY, private_key_path) : result; |
325 | 1.52k | } |
326 | 5.40k | const curl_blob certificate = {const_cast<char*>(tls_test_credentials::kCertificatePem), |
327 | 5.40k | sizeof(tls_test_credentials::kCertificatePem) - 1, CURL_BLOB_COPY}; |
328 | 5.40k | result = curl_easy_setopt(easy, CURLOPT_SSLCERT_BLOB, &certificate); |
329 | 5.40k | if (result != CURLE_OK) { |
330 | 18 | return result; |
331 | 18 | } |
332 | 5.38k | const curl_blob private_key = {const_cast<char*>(tls_test_credentials::kPrivateKeyPem), |
333 | 5.38k | sizeof(tls_test_credentials::kPrivateKeyPem) - 1, CURL_BLOB_COPY}; |
334 | 5.38k | return curl_easy_setopt(easy, CURLOPT_SSLKEY_BLOB, &private_key); |
335 | 5.40k | } |
336 | | |
337 | 0 | default: |
338 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
339 | 87.2k | } |
340 | 87.2k | } |
341 | | |
342 | | /// Translate and apply one generated scalar/string SetOption to the easy handle. |
343 | | /// Pointer-valued options borrow the SetOption's protobuf-owned string storage, |
344 | | /// so its containing Scenario must remain alive and unmodified until the |
345 | | /// transfer has stopped and the easy handle has been cleaned up. Borrowing |
346 | | /// avoids a duplicate backing allocation on every iteration; the lifetime is |
347 | | /// especially important for CURLOPT_POSTFIELDS, which curl does not copy. |
348 | | /// @param easy The curl easy handle to configure. |
349 | | /// @param option The SetOption proto describing which option and value to set. |
350 | | /// @return curl's setopt result, or CURLE_UNKNOWN_OPTION for an unknown id. |
351 | 1.31M | CURLcode ApplySetOption(CURL* easy, const curl::fuzzer::proto::SetOption& option) { |
352 | 1.31M | const OptionDescriptor* desc = LookupOptionDescriptor(option.option_id()); |
353 | 1.31M | if (desc == nullptr) { |
354 | 47.5k | return CURLE_UNKNOWN_OPTION; |
355 | 47.5k | } |
356 | 1.26M | const CURLoption curlopt = static_cast<CURLoption>(desc->curlopt); |
357 | | |
358 | 1.26M | switch (desc->kind) { |
359 | 518k | case OptionValueKind::kString: { |
360 | 518k | const std::string& value = option.string_value(); |
361 | | |
362 | | // COPYPOSTFIELDS allocates after reading the separately configured |
363 | | // POSTFIELDSIZE. First install the same protobuf-owned bytes as a |
364 | | // no-allocation borrowed fallback. A genuine OOM then still leaves a |
365 | | // coherent pointer and length for later consumers such as AWS SigV4. |
366 | 518k | if (desc->curlopt == CURLOPT_COPYPOSTFIELDS) { |
367 | 27.6k | CURLcode result = curl_easy_setopt(easy, CURLOPT_POSTFIELDS, value.c_str()); |
368 | 27.6k | if (result != CURLE_OK) { |
369 | 0 | return result; |
370 | 0 | } |
371 | 27.6k | } |
372 | | |
373 | | // POSTFIELDS borrows its pointer and COPYPOSTFIELDS copies exactly the |
374 | | // previously configured size. Apply the correlated byte length first so |
375 | | // either option accepts embedded NULs without strlen semantics and the |
376 | | // copying variant can never read beyond the protobuf-owned buffer. |
377 | 518k | if (desc->curlopt == CURLOPT_POSTFIELDS || desc->curlopt == CURLOPT_COPYPOSTFIELDS) { |
378 | 36.2k | CURLcode result = curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(value.size())); |
379 | 36.2k | if (result != CURLE_OK) { |
380 | 0 | return result; |
381 | 0 | } |
382 | 36.2k | } |
383 | | |
384 | 518k | if (desc->curlopt == CURLOPT_COPYPOSTFIELDS) { |
385 | | // Copy ownership is a harness invariant rather than an allocation- |
386 | | // failure target. Preserve pending ordinals for later injectable work. |
387 | 27.6k | allocation_fault_injection::ScopedFailureSuppression suppress_failures; |
388 | 27.6k | return curl_easy_setopt(easy, CURLOPT_COPYPOSTFIELDS, value.c_str()); |
389 | 27.6k | } |
390 | | |
391 | | // Curl otherwise treats this option as a filename during the TLS |
392 | | // handshake. Fixed-policy inputs normally arrive pre-constrained by the |
393 | | // postprocessor; this runtime check also protects compatibility inputs, |
394 | | // which intentionally bypass it. Curl copies this option in setopt, so |
395 | | // the temporary remains valid for the call's full ownership contract. |
396 | 490k | if (desc->curlopt == CURLOPT_PINNEDPUBLICKEY && !UsesInMemoryPublicKeyPin(value)) { |
397 | 383 | std::string constrained = value; |
398 | 383 | ConstrainPinnedPublicKeyValue(&constrained); |
399 | 383 | return curl_easy_setopt(easy, curlopt, constrained.c_str()); |
400 | 383 | } |
401 | | |
402 | 489k | return curl_easy_setopt(easy, curlopt, value.c_str()); |
403 | 490k | } |
404 | | |
405 | | // Decode the uint_value and pass it as either a long or a curl_off_t depending on the option. |
406 | 481k | case OptionValueKind::kUint: { |
407 | 481k | const std::uint64_t raw = DecodeIntegralOptionValue(*desc, option); |
408 | | // CURLOPTTYPE_OFF_T options start at 30000. Everything below takes a |
409 | | // long; everything at/above takes a curl_off_t. |
410 | 481k | if (static_cast<int>(desc->curlopt) >= 30000) { |
411 | 55.9k | return curl_easy_setopt(easy, curlopt, static_cast<curl_off_t>(raw)); |
412 | 55.9k | } |
413 | 425k | return curl_easy_setopt(easy, curlopt, static_cast<long>(raw)); |
414 | 481k | } |
415 | | |
416 | | // Decode the bool_value and pass it as a long flag (0 or 1). |
417 | 265k | case OptionValueKind::kBool: { |
418 | 265k | const long flag = static_cast<long>(DecodeIntegralOptionValue(*desc, option)); |
419 | 265k | return curl_easy_setopt(easy, curlopt, flag); |
420 | 481k | } |
421 | 1.26M | } |
422 | 0 | return CURLE_UNKNOWN_OPTION; |
423 | 1.26M | } |
424 | | |
425 | | /// Return the runtime-visible option prefix length. Fixed targets normally |
426 | | /// trim the protobuf in their postprocessor, but the compatibility target must |
427 | | /// preserve its historical message unchanged; applying the same bound here |
428 | | /// prevents that lane from doing mutation-sized setopt work while exposing the |
429 | | /// same option prefix as postprocessed fixed lanes. |
430 | | /// @param scenario Structured input whose option prefix will be consumed. |
431 | | /// @return Number of options visible to the runtime. |
432 | 1.04M | std::size_t RuntimeOptionCount(const curl::fuzzer::proto::Scenario& scenario) { |
433 | 1.04M | return std::min<std::size_t>(static_cast<std::size_t>(scenario.options_size()), scenario_limits::kMaxOptions); |
434 | 1.04M | } |
435 | | |
436 | | /// Apply the bounded runtime-visible prefix of Scenario.options. Individual |
437 | | /// CURLcode values remain intentionally ignored, matching the fuzzer runner's |
438 | | /// historical behavior. Bounding here also covers standalone compatibility |
439 | | /// seeds that reach RunScenario without normalization. |
440 | | /// @param easy Easy handle receiving each supported option. |
441 | | /// @param scenario Structured input that owns all borrowed option strings. |
442 | | /// @return Number of option entries attempted. |
443 | 492k | std::size_t ApplyScenarioOptions(CURL* easy, const curl::fuzzer::proto::Scenario& scenario) { |
444 | 492k | const std::size_t option_count = RuntimeOptionCount(scenario); |
445 | 1.80M | for (std::size_t index = 0; index < option_count; ++index) { |
446 | 1.31M | (void)ApplySetOption(easy, scenario.options(static_cast<int>(index))); |
447 | 1.31M | } |
448 | 492k | return option_count; |
449 | 492k | } |
450 | | |
451 | | } // namespace proto_fuzzer |