/src/curl_fuzzer/proto_fuzzer/request_data.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 Builds bounded curl_slist and curl_mime state from a Scenario. |
9 | | |
10 | | #include "proto_fuzzer/request_data.h" |
11 | | |
12 | | #include <algorithm> |
13 | | #include <cstddef> |
14 | | #include <cstdio> |
15 | | #include <cstring> |
16 | | #include <limits> |
17 | | #include <string> |
18 | | |
19 | | #include "proto_fuzzer/option_apply.h" |
20 | | #include "proto_fuzzer/option_model.h" |
21 | | #include "proto_fuzzer/scenario_limits.h" |
22 | | |
23 | | namespace proto_fuzzer { |
24 | | |
25 | | namespace { |
26 | | |
27 | | /// Borrow the protobuf string when it already fits curl's NUL-terminated API, |
28 | | /// allocating `truncated` only for compatibility inputs that bypass the fixed |
29 | | /// target postprocessor. The callers below all synchronously copy this value, |
30 | | /// so neither pointer escapes the call. Embedded NUL bytes deliberately remain: |
31 | | /// curl observes the same prefix as before while an oversized invisible suffix |
32 | | /// cannot dominate allocation. |
33 | 477k | const char* BoundedCString(const std::string& value, std::size_t limit, std::string* truncated) { |
34 | 477k | if (value.size() <= limit) { |
35 | 477k | return value.c_str(); |
36 | 477k | } |
37 | 112 | truncated->assign(value.data(), limit); |
38 | 112 | return truncated->c_str(); |
39 | 477k | } |
40 | | |
41 | | /// Raw read/seek callbacks are useful only when a script exists or a supported |
42 | | /// SetOption can make curl request caller-provided body bytes. CURLOPT_POST is |
43 | | /// included because POST without POSTFIELDS/MIME also falls back to the read |
44 | | /// callback. Avoiding callbacks for ordinary requests removes eight setopt |
45 | | /// calls across setup and teardown, while any potentially body-reading option |
46 | | /// retains the non-blocking fallback regardless of its mutated value. |
47 | 491k | bool NeedsUploadCallbacks(const curl::fuzzer::proto::Scenario& scenario) { |
48 | | // curl's TELNET implementation polls stdin unless a READFUNCTION was |
49 | | // explicitly installed. Always provide the bounded per-scenario source, |
50 | | // even when there is no upload payload to send. |
51 | 491k | if (scenario.scheme() == curl::fuzzer::proto::SCHEME_TELNET || scenario.has_upload()) { |
52 | 47.4k | return true; |
53 | 47.4k | } |
54 | 443k | const std::size_t option_count = RuntimeOptionCount(scenario); |
55 | 1.49M | for (std::size_t index = 0; index < option_count; ++index) { |
56 | 1.05M | const auto& option = scenario.options(static_cast<int>(index)); |
57 | 1.05M | if (option.option_id() == curl::fuzzer::proto::CURLOPT_UPLOAD || |
58 | 1.04M | option.option_id() == curl::fuzzer::proto::CURLOPT_POST) { |
59 | 11.5k | return true; |
60 | 11.5k | } |
61 | 1.05M | } |
62 | 432k | return false; |
63 | 443k | } |
64 | | |
65 | | /// Return whether a Deep HTTP selector routes the transfer through proxy |
66 | | /// handling. Non-default proxy/socket modes own their own route even when the |
67 | | /// selected value ultimately bypasses a configured proxy. |
68 | 12.2k | bool UsesHttpProxyRoute(const curl::fuzzer::proto::Scenario& scenario) { |
69 | 12.2k | if (scenario.proxy_socket_option_mode() != curl::fuzzer::proto::PROXY_SOCKET_OPTION_DEFAULT) { |
70 | 106 | return true; |
71 | 106 | } |
72 | 12.1k | return scenario.http_transport_mode() == curl::fuzzer::proto::HTTP_TRANSPORT_FORWARD_PROXY || |
73 | 12.1k | scenario.http_transport_mode() == curl::fuzzer::proto::HTTP_TRANSPORT_CONNECT_PROXY; |
74 | 12.2k | } |
75 | | |
76 | | /// Determine whether the effective HTTP version can invoke the HTTP/1.1 |
77 | | /// trailer callback. The last retained CURLOPT_HTTP_VERSION wins, matching |
78 | | /// ApplyScenarioOptions. HTTP's 2TLS mode remains HTTP/1.1 without TLS. |
79 | 12.4k | bool SupportsHttpUploadTrailers(const curl::fuzzer::proto::Scenario& scenario) { |
80 | 12.4k | if ((scenario.scheme() != curl::fuzzer::proto::SCHEME_HTTP && |
81 | 10.3k | scenario.scheme() != curl::fuzzer::proto::SCHEME_HTTPS) || |
82 | 12.2k | UsesHttpProxyRoute(scenario)) { |
83 | 306 | return false; |
84 | 306 | } |
85 | | |
86 | 12.1k | std::uint64_t version = CURL_HTTP_VERSION_NONE; |
87 | 12.1k | const std::size_t option_count = RuntimeOptionCount(scenario); |
88 | 59.9k | for (std::size_t index = 0; index < option_count; ++index) { |
89 | 47.8k | const auto& option = scenario.options(static_cast<int>(index)); |
90 | 47.8k | if (option.option_id() == curl::fuzzer::proto::CURLOPT_HTTP_VERSION) { |
91 | 4.64k | version = DecodeIntegralOptionValue(option); |
92 | 4.64k | } |
93 | 47.8k | } |
94 | 12.1k | return version == CURL_HTTP_VERSION_NONE || version == CURL_HTTP_VERSION_1_1 || |
95 | 3.11k | (scenario.scheme() == curl::fuzzer::proto::SCHEME_HTTP && version == CURL_HTTP_VERSION_2TLS); |
96 | 12.4k | } |
97 | | |
98 | | /// Translate the mutation-friendly enum to curl's spelling. Restricting this |
99 | | /// field to supported encoders spends cycles in encoder implementations rather |
100 | | /// than repeatedly rediscovering the same invalid-string rejection. |
101 | 217k | const char* MimeEncoderName(curl::fuzzer::proto::MimeEncoder encoder) { |
102 | 217k | switch (encoder) { |
103 | 11.7k | case curl::fuzzer::proto::MIME_ENCODER_BINARY: |
104 | 11.7k | return "binary"; |
105 | 11.1k | case curl::fuzzer::proto::MIME_ENCODER_8BIT: |
106 | 11.1k | return "8bit"; |
107 | 13.5k | case curl::fuzzer::proto::MIME_ENCODER_7BIT: |
108 | 13.5k | return "7bit"; |
109 | 25.5k | case curl::fuzzer::proto::MIME_ENCODER_BASE64: |
110 | 25.5k | return "base64"; |
111 | 27.4k | case curl::fuzzer::proto::MIME_ENCODER_QUOTED_PRINTABLE: |
112 | 27.4k | return "quoted-printable"; |
113 | 127k | case curl::fuzzer::proto::MIME_ENCODER_UNSPECIFIED: |
114 | 128k | default: |
115 | 128k | return nullptr; |
116 | 217k | } |
117 | 217k | } |
118 | | |
119 | | /// Append at most `limit` protobuf byte strings to a curl list. curl_slist_append |
120 | | /// leaves the old head valid on allocation failure, so only replace the head |
121 | | /// after a successful append and stop rather than burning the rest of the |
122 | | /// iteration on allocations that are already failing. |
123 | | template <typename RepeatedBytes> |
124 | | CurlSlistPtr BuildStringList(const RepeatedBytes& values, std::size_t count_limit, std::size_t value_limit, |
125 | 758k | std::size_t* applied) { |
126 | 758k | CurlSlistPtr list; |
127 | 758k | std::string truncated; |
128 | 758k | const std::size_t count = std::min<std::size_t>(count_limit, values.size()); |
129 | 1.08M | for (std::size_t i = 0; i < count; ++i) { |
130 | 330k | const std::string& value = values.Get(static_cast<int>(i)); |
131 | 330k | curl_slist* appended = curl_slist_append(list.get(), BoundedCString(value, value_limit, &truncated)); |
132 | 330k | if (appended == nullptr) { |
133 | 652 | break; |
134 | 652 | } |
135 | | // append retains the existing nodes and may return the same head. Release |
136 | | // before resetting so adopting the successful result cannot free them. |
137 | 329k | (void)list.release(); |
138 | 329k | list.reset(appended); |
139 | 329k | ++*applied; |
140 | 329k | } |
141 | 758k | return list; |
142 | 758k | } |
143 | | |
144 | | /// Apply the metadata shared by top-level and nested protobuf part types. The |
145 | | /// MIME API copies these strings, so temporary bounded buffers are sufficient; |
146 | | /// only the MIME root itself needs to outlive the perform loop. |
147 | | template <typename ProtoPart> |
148 | 217k | void ApplyPartMetadata(curl_mimepart* part, const ProtoPart& source, RequestBuildStats* stats) { |
149 | | // Reuse the rare compatibility-path allocation across all three fields; |
150 | | // ordinary postprocessed metadata never writes this scratch string. |
151 | 217k | std::string truncated; |
152 | 217k | if (!source.name().empty()) { |
153 | 49.2k | (void)curl_mime_name(part, BoundedCString(source.name(), scenario_limits::kMaxMetadataBytes, &truncated)); |
154 | 49.2k | } |
155 | 217k | if (!source.filename().empty()) { |
156 | 51.3k | (void)curl_mime_filename(part, BoundedCString(source.filename(), scenario_limits::kMaxMetadataBytes, &truncated)); |
157 | 51.3k | } |
158 | 217k | if (!source.content_type().empty()) { |
159 | 46.7k | (void)curl_mime_type(part, BoundedCString(source.content_type(), scenario_limits::kMaxMetadataBytes, &truncated)); |
160 | 46.7k | } |
161 | 217k | if (const char* encoder = MimeEncoderName(source.encoder())) { |
162 | 89.5k | (void)curl_mime_encoder(part, encoder); |
163 | 89.5k | } |
164 | | |
165 | 217k | std::size_t header_count = 0; |
166 | 217k | CurlSlistPtr headers = BuildStringList(source.headers(), scenario_limits::kMaxMimeHeadersPerPart, |
167 | 217k | scenario_limits::kMaxMetadataBytes, &header_count); |
168 | 217k | if (headers != nullptr) { |
169 | | // take_ownership=1 is crucial: unlike the strings above, MIME retains the |
170 | | // list pointer. Once attached, the root curl_mime_free call recursively |
171 | | // releases it, including lists on nested parts. |
172 | 45.7k | const CURLcode result = curl_mime_headers(part, headers.get(), 1); |
173 | 45.7k | if (result == CURLE_OK) { |
174 | 45.7k | (void)headers.release(); |
175 | 45.7k | stats->mime_headers += header_count; |
176 | 45.7k | } |
177 | 45.7k | } |
178 | 217k | } request_data.cc:void proto_fuzzer::(anonymous namespace)::ApplyPartMetadata<curl::fuzzer::proto::MimePart>(curl_mimepart*, curl::fuzzer::proto::MimePart const&, proto_fuzzer::RequestBuildStats*) Line | Count | Source | 148 | 132k | void ApplyPartMetadata(curl_mimepart* part, const ProtoPart& source, RequestBuildStats* stats) { | 149 | | // Reuse the rare compatibility-path allocation across all three fields; | 150 | | // ordinary postprocessed metadata never writes this scratch string. | 151 | 132k | std::string truncated; | 152 | 132k | if (!source.name().empty()) { | 153 | 29.4k | (void)curl_mime_name(part, BoundedCString(source.name(), scenario_limits::kMaxMetadataBytes, &truncated)); | 154 | 29.4k | } | 155 | 132k | if (!source.filename().empty()) { | 156 | 28.4k | (void)curl_mime_filename(part, BoundedCString(source.filename(), scenario_limits::kMaxMetadataBytes, &truncated)); | 157 | 28.4k | } | 158 | 132k | if (!source.content_type().empty()) { | 159 | 26.4k | (void)curl_mime_type(part, BoundedCString(source.content_type(), scenario_limits::kMaxMetadataBytes, &truncated)); | 160 | 26.4k | } | 161 | 132k | if (const char* encoder = MimeEncoderName(source.encoder())) { | 162 | 56.4k | (void)curl_mime_encoder(part, encoder); | 163 | 56.4k | } | 164 | | | 165 | 132k | std::size_t header_count = 0; | 166 | 132k | CurlSlistPtr headers = BuildStringList(source.headers(), scenario_limits::kMaxMimeHeadersPerPart, | 167 | 132k | scenario_limits::kMaxMetadataBytes, &header_count); | 168 | 132k | if (headers != nullptr) { | 169 | | // take_ownership=1 is crucial: unlike the strings above, MIME retains the | 170 | | // list pointer. Once attached, the root curl_mime_free call recursively | 171 | | // releases it, including lists on nested parts. | 172 | 24.7k | const CURLcode result = curl_mime_headers(part, headers.get(), 1); | 173 | 24.7k | if (result == CURLE_OK) { | 174 | 24.7k | (void)headers.release(); | 175 | 24.7k | stats->mime_headers += header_count; | 176 | 24.7k | } | 177 | 24.7k | } | 178 | 132k | } |
request_data.cc:void proto_fuzzer::(anonymous namespace)::ApplyPartMetadata<curl::fuzzer::proto::MimeDataPart>(curl_mimepart*, curl::fuzzer::proto::MimeDataPart const&, proto_fuzzer::RequestBuildStats*) Line | Count | Source | 148 | 85.5k | void ApplyPartMetadata(curl_mimepart* part, const ProtoPart& source, RequestBuildStats* stats) { | 149 | | // Reuse the rare compatibility-path allocation across all three fields; | 150 | | // ordinary postprocessed metadata never writes this scratch string. | 151 | 85.5k | std::string truncated; | 152 | 85.5k | if (!source.name().empty()) { | 153 | 19.8k | (void)curl_mime_name(part, BoundedCString(source.name(), scenario_limits::kMaxMetadataBytes, &truncated)); | 154 | 19.8k | } | 155 | 85.5k | if (!source.filename().empty()) { | 156 | 22.9k | (void)curl_mime_filename(part, BoundedCString(source.filename(), scenario_limits::kMaxMetadataBytes, &truncated)); | 157 | 22.9k | } | 158 | 85.5k | if (!source.content_type().empty()) { | 159 | 20.2k | (void)curl_mime_type(part, BoundedCString(source.content_type(), scenario_limits::kMaxMetadataBytes, &truncated)); | 160 | 20.2k | } | 161 | 85.5k | if (const char* encoder = MimeEncoderName(source.encoder())) { | 162 | 33.1k | (void)curl_mime_encoder(part, encoder); | 163 | 33.1k | } | 164 | | | 165 | 85.5k | std::size_t header_count = 0; | 166 | 85.5k | CurlSlistPtr headers = BuildStringList(source.headers(), scenario_limits::kMaxMimeHeadersPerPart, | 167 | 85.5k | scenario_limits::kMaxMetadataBytes, &header_count); | 168 | 85.5k | if (headers != nullptr) { | 169 | | // take_ownership=1 is crucial: unlike the strings above, MIME retains the | 170 | | // list pointer. Once attached, the root curl_mime_free call recursively | 171 | | // releases it, including lists on nested parts. | 172 | 21.0k | const CURLcode result = curl_mime_headers(part, headers.get(), 1); | 173 | 21.0k | if (result == CURLE_OK) { | 174 | 21.0k | (void)headers.release(); | 175 | 21.0k | stats->mime_headers += header_count; | 176 | 21.0k | } | 177 | 21.0k | } | 178 | 85.5k | } |
|
179 | | |
180 | | /// Copy bounded binary data into a MIME part. curl_mime_data accepts an |
181 | | /// explicit size, so embedded NUL bytes remain fuzzable here unlike in the |
182 | | /// metadata and header APIs. |
183 | 79.7k | void ApplyPartData(curl_mimepart* part, const std::string& data) { |
184 | 79.7k | const std::size_t size = std::min(data.size(), scenario_limits::kMaxMimeDataBytes); |
185 | 79.7k | const char* bytes = data.empty() ? "" : data.data(); |
186 | 79.7k | (void)curl_mime_data(part, bytes, size); |
187 | 79.7k | } |
188 | | |
189 | | /// Materialize one compact repeated source without allowing the protobuf's |
190 | | /// uint32 count or a collection of MIME parts to amplify into unbounded work. |
191 | | /// Division establishes the maximum count before multiplication, so this is |
192 | | /// safe even when size_t is narrower than uint32_t. |
193 | | void ApplyGeneratedPartData(curl_mimepart* part, const curl::fuzzer::proto::GeneratedBytes& source, |
194 | 32.7k | std::size_t* remaining_generated_bytes, RequestBuildStats* stats) { |
195 | 32.7k | const std::size_t pattern_size = std::min(source.pattern().size(), scenario_limits::kMaxGeneratedMimePatternBytes); |
196 | 32.7k | if (pattern_size == 0 || *remaining_generated_bytes == 0) { |
197 | 10.9k | (void)curl_mime_data(part, "", 0); |
198 | 10.9k | return; |
199 | 10.9k | } |
200 | | |
201 | 21.8k | const std::size_t max_repeat_count = *remaining_generated_bytes / pattern_size; |
202 | 21.8k | const std::size_t repeat_count = |
203 | 21.8k | std::min<std::size_t>(static_cast<std::size_t>(source.repeat_count()), max_repeat_count); |
204 | 21.8k | const std::size_t materialized_size = repeat_count * pattern_size; |
205 | | |
206 | 21.8k | std::string materialized(materialized_size, '\0'); |
207 | 353M | for (std::size_t offset = 0; offset < materialized_size; offset += pattern_size) { |
208 | 353M | std::copy_n(source.pattern().data(), pattern_size, materialized.data() + offset); |
209 | 353M | } |
210 | 21.8k | const char* bytes = materialized.empty() ? "" : materialized.data(); |
211 | 21.8k | if (curl_mime_data(part, bytes, materialized.size()) == CURLE_OK) { |
212 | 21.7k | stats->generated_mime_bytes += materialized.size(); |
213 | 21.7k | } |
214 | 21.8k | *remaining_generated_bytes -= materialized.size(); |
215 | 21.8k | } |
216 | | |
217 | | /// Populate the fixed-depth child body and debit the shared total-part budget. |
218 | | /// Returning an empty MIME object when the child list is empty is intentional: |
219 | | /// curl's empty multipart serialization is useful coverage and remains cheap. |
220 | | void PopulateSubparts(curl_mime* mime, const curl::fuzzer::proto::MimeSubparts& source, std::size_t* remaining_parts, |
221 | 42.1k | std::size_t* remaining_generated_bytes, RequestBuildStats* stats) { |
222 | 42.1k | const std::size_t count = std::min<std::size_t>(scenario_limits::kMaxNestedMimeParts, source.parts_size()); |
223 | 127k | for (std::size_t i = 0; i < count && *remaining_parts != 0; ++i) { |
224 | 85.6k | curl_mimepart* part = curl_mime_addpart(mime); |
225 | 85.6k | if (part == nullptr) { |
226 | 140 | break; |
227 | 140 | } |
228 | 85.5k | --*remaining_parts; |
229 | 85.5k | ++stats->mime_parts; |
230 | 85.5k | const auto& proto_part = source.parts(static_cast<int>(i)); |
231 | 85.5k | ApplyPartMetadata(part, proto_part, stats); |
232 | 85.5k | if (proto_part.content_case() == curl::fuzzer::proto::MimeDataPart::kGeneratedData) { |
233 | 13.2k | ApplyGeneratedPartData(part, proto_part.generated_data(), remaining_generated_bytes, stats); |
234 | 72.2k | } else { |
235 | | // Preserve the historical leaf behavior: an absent source is applied as |
236 | | // empty data just like an explicitly empty field 2. |
237 | 72.2k | ApplyPartData(part, proto_part.data()); |
238 | 72.2k | } |
239 | 85.5k | } |
240 | 42.1k | } |
241 | | |
242 | | /// Construct the top MIME tree. curl_mime_subparts transfers ownership only |
243 | | /// on success, so failed attachments are freed immediately while successful |
244 | | /// ones are left for the top-level root to release recursively. |
245 | | CurlMimePtr BuildMimePost(CURL* easy, const curl::fuzzer::proto::MimePost& source, UploadScriptState* upload_state, |
246 | 81.7k | RequestBuildStats* stats) { |
247 | 81.7k | CurlMimePtr mime(curl_mime_init(easy)); |
248 | 81.7k | if (mime == nullptr) { |
249 | 141 | return nullptr; |
250 | 141 | } |
251 | | |
252 | 81.6k | std::size_t remaining_parts = scenario_limits::kMaxTotalMimeParts; |
253 | 81.6k | std::size_t remaining_generated_bytes = scenario_limits::kMaxGeneratedMimeDataBytes; |
254 | 81.6k | const std::size_t count = std::min<std::size_t>(scenario_limits::kMaxTopLevelMimeParts, source.parts_size()); |
255 | 213k | for (std::size_t i = 0; i < count && remaining_parts != 0; ++i) { |
256 | 132k | curl_mimepart* part = curl_mime_addpart(mime.get()); |
257 | 132k | if (part == nullptr) { |
258 | 196 | break; |
259 | 196 | } |
260 | 132k | --remaining_parts; |
261 | 132k | ++stats->mime_parts; |
262 | 132k | const auto& proto_part = source.parts(static_cast<int>(i)); |
263 | 132k | ApplyPartMetadata(part, proto_part, stats); |
264 | | |
265 | 132k | switch (proto_part.content_case()) { |
266 | 7.51k | case curl::fuzzer::proto::MimePart::kData: |
267 | 7.51k | ApplyPartData(part, proto_part.data()); |
268 | 7.51k | break; |
269 | 19.4k | case curl::fuzzer::proto::MimePart::kGeneratedData: |
270 | 19.4k | ApplyGeneratedPartData(part, proto_part.generated_data(), &remaining_generated_bytes, stats); |
271 | 19.4k | break; |
272 | 22.7k | case curl::fuzzer::proto::MimePart::kUploadCallback: |
273 | 22.7k | if (upload_state != nullptr && upload_state->InstallAsMimePart(part) == CURLE_OK) { |
274 | 22.7k | ++stats->mime_callback_parts; |
275 | 22.7k | } |
276 | 22.7k | break; |
277 | 42.3k | case curl::fuzzer::proto::MimePart::kSubparts: { |
278 | 42.3k | CurlMimePtr subparts(curl_mime_init(easy)); |
279 | 42.3k | if (subparts == nullptr) { |
280 | 118 | break; |
281 | 118 | } |
282 | 42.1k | PopulateSubparts(subparts.get(), proto_part.subparts(), &remaining_parts, &remaining_generated_bytes, stats); |
283 | 42.1k | if (curl_mime_subparts(part, subparts.get()) == CURLE_OK) { |
284 | 42.1k | (void)subparts.release(); |
285 | 42.1k | } |
286 | 42.1k | break; |
287 | 42.3k | } |
288 | 40.3k | case curl::fuzzer::proto::MimePart::CONTENT_NOT_SET: |
289 | 40.3k | default: |
290 | 40.3k | break; |
291 | 132k | } |
292 | 132k | } |
293 | 81.6k | return mime; |
294 | 81.6k | } |
295 | | |
296 | | } // namespace |
297 | | |
298 | | /// Borrow and cap a scripted upload source before libcurl receives a userdata |
299 | | /// pointer. When Scenario.upload is absent, non-TELNET schemes synthesize the |
300 | | /// historical 16 KiB `U` stream without allocating it, while TELNET reports |
301 | | /// immediate EOF so merely selecting the protocol cannot inject unsolicited |
302 | | /// terminal input. Borrowing avoids duplicating every fuzzed body before curl |
303 | | /// can read it; `scenario` must remain alive and unmodified until this state is |
304 | | /// destroyed. |
305 | | /// @param scenario Source and lifetime owner of callback bytes and outcomes. |
306 | | UploadScriptState::UploadScriptState(const curl::fuzzer::proto::Scenario& scenario) |
307 | 491k | : data_(), |
308 | 491k | read_step_count_(0), |
309 | 491k | total_size_(scenario.scheme() == curl::fuzzer::proto::SCHEME_TELNET ? 0 : scenario_limits::kMaxUploadBytes), |
310 | 491k | max_read_size_(scenario.scheme() == curl::fuzzer::proto::SCHEME_TELNET ? scenario_limits::kMaxTelnetUploadReadSize |
311 | 491k | : scenario_limits::kMaxUploadReadSize), |
312 | 491k | offset_(0), |
313 | 491k | next_read_size_(0), |
314 | 491k | seek_calls_(0), |
315 | 491k | terminal_(curl::fuzzer::proto::UPLOAD_TERMINAL_EOF), |
316 | 491k | seek_result_(curl::fuzzer::proto::UPLOAD_SEEK_CANTSEEK), |
317 | 491k | before_read_callback_(nullptr), |
318 | 491k | before_read_userdata_(nullptr), |
319 | 491k | scripted_(scenario.has_upload()) { |
320 | 491k | if (!scripted_) { |
321 | 449k | return; |
322 | 449k | } |
323 | | |
324 | 41.8k | const auto& upload = scenario.upload(); |
325 | 41.8k | const std::size_t data_limit = scenario.scheme() == curl::fuzzer::proto::SCHEME_TELNET |
326 | 41.8k | ? scenario_limits::kMaxTelnetUploadBytes |
327 | 41.8k | : scenario_limits::kMaxUploadBytes; |
328 | 41.8k | const std::size_t data_size = std::min(upload.data().size(), data_limit); |
329 | 41.8k | data_ = std::string_view(upload.data().data(), data_size); |
330 | 41.8k | total_size_ = data_.size(); |
331 | 41.8k | terminal_ = upload.terminal(); |
332 | 41.8k | if (scenario.scheme() != curl::fuzzer::proto::SCHEME_TELNET && |
333 | 41.5k | terminal_ == curl::fuzzer::proto::UPLOAD_TERMINAL_PAUSE) { |
334 | | // Event-driven protocols need an external resume source. Interpret this |
335 | | // TELNET-specific outcome as EOF before curl can retain a paused transfer. |
336 | 119 | terminal_ = curl::fuzzer::proto::UPLOAD_TERMINAL_EOF; |
337 | 119 | } |
338 | 41.8k | seek_result_ = upload.seek_result(); |
339 | | |
340 | 41.8k | const std::size_t read_step_limit = scenario.scheme() == curl::fuzzer::proto::SCHEME_TELNET |
341 | 41.8k | ? scenario_limits::kMaxTelnetUploadReadSteps |
342 | 41.8k | : scenario_limits::kMaxUploadReadSteps; |
343 | 41.8k | read_step_count_ = std::min<std::size_t>(upload.read_sizes_size(), read_step_limit); |
344 | 107k | for (std::size_t i = 0; i < read_step_count_; ++i) { |
345 | 66.0k | const std::size_t requested = upload.read_sizes(static_cast<int>(i)); |
346 | | // Zero-as-one ensures every retained step makes progress; see the schema |
347 | | // comment for why zero is not treated as an early EOF sentinel. |
348 | 66.0k | read_sizes_[i] = std::max<std::size_t>(1, std::min(requested, max_read_size_)); |
349 | 66.0k | } |
350 | 41.8k | } |
351 | | |
352 | | /// Copy the next bounded chunk and advance the cursor, using either the explicit |
353 | | /// payload or the allocation-free fallback. Terminal outcomes are emitted only |
354 | | /// after all data is consumed so a mutation can independently control |
355 | | /// fragmentation and completion policy. Public so focused tests can validate |
356 | | /// callback semantics without relying on HTTP timing. |
357 | | /// @param buffer Destination supplied by curl. |
358 | | /// @param capacity Writable bytes in `buffer`. |
359 | | /// @return Bytes copied, zero for EOF, CURL_READFUNC_ABORT, or the TELNET- |
360 | | /// only CURL_READFUNC_PAUSE outcome. |
361 | 271k | std::size_t UploadScriptState::Read(char* buffer, std::size_t capacity) { |
362 | | // TELNET can produce negotiation replies while one curl_multi_perform call |
363 | | // owns the thread. Empty them before returning more callback bytes; |
364 | | // otherwise send_telnet_data() can consume the whole bounded transfer |
365 | | // timeout while the synchronous path waits for socket capacity. |
366 | 271k | if (before_read_callback_ != nullptr) { |
367 | 1.78k | before_read_callback_(before_read_userdata_); |
368 | 1.78k | } |
369 | 271k | if (offset_ >= total_size_) { |
370 | 20.1k | switch (terminal_) { |
371 | 532 | case curl::fuzzer::proto::UPLOAD_TERMINAL_ABORT: |
372 | 532 | return CURL_READFUNC_ABORT; |
373 | 23 | case curl::fuzzer::proto::UPLOAD_TERMINAL_PAUSE: |
374 | 23 | return CURL_READFUNC_PAUSE; |
375 | 19.5k | case curl::fuzzer::proto::UPLOAD_TERMINAL_EOF: |
376 | 19.6k | default: |
377 | 19.6k | return 0; |
378 | 20.1k | } |
379 | 20.1k | } |
380 | 250k | if (buffer == nullptr || capacity == 0) { |
381 | 0 | return 0; |
382 | 0 | } |
383 | | |
384 | 250k | std::size_t chunk_limit = std::min(capacity, max_read_size_); |
385 | 250k | if (next_read_size_ < read_step_count_) { |
386 | 36.7k | chunk_limit = std::min(chunk_limit, read_sizes_[next_read_size_++]); |
387 | 36.7k | } |
388 | 250k | const std::size_t count = std::min(chunk_limit, total_size_ - offset_); |
389 | 250k | if (scripted_) { |
390 | 50.9k | std::memcpy(buffer, data_.data() + offset_, count); |
391 | 199k | } else { |
392 | 199k | std::memset(buffer, 'U', count); |
393 | 199k | } |
394 | 250k | offset_ += count; |
395 | 250k | return count; |
396 | 250k | } |
397 | | |
398 | | /// Apply the configured seek outcome and, for OK, move the bounded cursor. |
399 | | /// Successful seeks restart the short-read sequence so retries replay the same |
400 | | /// callback fragmentation deterministically. Explicit range checks model only |
401 | | /// seeks curl can meaningfully request from a bounded memory source, avoiding |
402 | | /// signed overflow or a mutation wrapping into an in-bounds cursor. |
403 | | /// @param requested_offset Offset interpreted relative to `origin`. |
404 | | /// @param origin One of SEEK_SET, SEEK_CUR, or SEEK_END. |
405 | | /// @return A CURL_SEEKFUNC_* result. |
406 | 23.6k | int UploadScriptState::Seek(curl_off_t requested_offset, int origin) { |
407 | 23.6k | ++seek_calls_; |
408 | 23.6k | switch (seek_result_) { |
409 | 1.24k | case curl::fuzzer::proto::UPLOAD_SEEK_CANTSEEK: |
410 | 1.24k | return CURL_SEEKFUNC_CANTSEEK; |
411 | 454 | case curl::fuzzer::proto::UPLOAD_SEEK_FAIL: |
412 | 454 | return CURL_SEEKFUNC_FAIL; |
413 | 21.9k | case curl::fuzzer::proto::UPLOAD_SEEK_OK: |
414 | 21.9k | break; |
415 | 1 | default: |
416 | 1 | return CURL_SEEKFUNC_CANTSEEK; |
417 | 23.6k | } |
418 | | |
419 | 21.9k | const curl_off_t current = static_cast<curl_off_t>(offset_); |
420 | 21.9k | const curl_off_t end = static_cast<curl_off_t>(total_size_); |
421 | 21.9k | curl_off_t base = 0; |
422 | 21.9k | switch (origin) { |
423 | 21.9k | case SEEK_SET: |
424 | 21.9k | base = 0; |
425 | 21.9k | break; |
426 | 0 | case SEEK_CUR: |
427 | 0 | base = current; |
428 | 0 | break; |
429 | 0 | case SEEK_END: |
430 | 0 | base = end; |
431 | 0 | break; |
432 | 0 | default: |
433 | 0 | return CURL_SEEKFUNC_FAIL; |
434 | 21.9k | } |
435 | | |
436 | | // Both base and end are at most 16 KiB. Comparing the requested delta to |
437 | | // these small bounds before addition handles even CURL_OFF_T_MIN safely. |
438 | 21.9k | if (requested_offset < -base || requested_offset > end - base) { |
439 | 90 | return CURL_SEEKFUNC_FAIL; |
440 | 90 | } |
441 | 21.8k | offset_ = static_cast<std::size_t>(base + requested_offset); |
442 | 21.8k | next_read_size_ = 0; |
443 | 21.8k | return CURL_SEEKFUNC_OK; |
444 | 21.9k | } |
445 | | |
446 | | /// libcurl trampoline that protects multiplication of its two buffer-size |
447 | | /// arguments before forwarding to Read(). Multiplication is normally benign |
448 | | /// because curl uses size=1, but callbacks are an API boundary. Abort an |
449 | | /// impossible overflowing pair: saturating to SIZE_MAX would let Read() copy |
450 | | /// into a buffer whose real extent is unknown. |
451 | 271k | std::size_t UploadScriptState::ReadCallback(char* buffer, std::size_t size, std::size_t nitems, void* userdata) { |
452 | 271k | if (userdata == nullptr || size == 0 || nitems == 0) { |
453 | 0 | return 0; |
454 | 0 | } |
455 | 271k | const std::size_t max = std::numeric_limits<std::size_t>::max(); |
456 | 271k | if (nitems > max / size) { |
457 | 0 | return CURL_READFUNC_ABORT; |
458 | 0 | } |
459 | 271k | return static_cast<UploadScriptState*>(userdata)->Read(buffer, size * nitems); |
460 | 271k | } |
461 | | |
462 | | /// libcurl trampoline for the userdata-first seek callback signature. Keep the |
463 | | /// C callback a type bridge so all outcome/cursor behaviour remains directly |
464 | | /// unit-testable in Seek(). |
465 | 23.6k | int UploadScriptState::SeekCallback(void* userdata, curl_off_t offset, int origin) { |
466 | 23.6k | if (userdata == nullptr) { |
467 | 0 | return CURL_SEEKFUNC_FAIL; |
468 | 0 | } |
469 | 23.6k | return static_cast<UploadScriptState*>(userdata)->Seek(offset, origin); |
470 | 23.6k | } |
471 | | |
472 | | /// @return Number of bytes visible to the callback after caps. |
473 | 354k | std::size_t UploadScriptState::data_size() const { return total_size_; } |
474 | | |
475 | | /// @return Number of retained per-read limits after caps. |
476 | 354k | std::size_t UploadScriptState::read_step_count() const { return read_step_count_; } |
477 | | |
478 | | /// @return Current byte cursor, exposed for deterministic unit tests. |
479 | 354k | std::size_t UploadScriptState::offset() const { return offset_; } |
480 | | |
481 | | /// @return Number of times libcurl asked the upload source to seek. |
482 | 354k | std::size_t UploadScriptState::seek_calls() const { return seek_calls_; } |
483 | | |
484 | | /// @return Whether Scenario.upload was present instead of using fallback. |
485 | 354k | bool UploadScriptState::scripted() const { return scripted_; } |
486 | | |
487 | | /// Install the same bounded read/seek cursor behind a public MIME callback |
488 | | /// part. curl retains `this` until the MIME root is freed, so ScenarioRequestData |
489 | | /// declares the root after the state and detaches it before either is released. |
490 | | /// @param part MIME part owned by the caller's live root. |
491 | | /// @return libcurl's setup result. |
492 | 22.7k | CURLcode UploadScriptState::InstallAsMimePart(curl_mimepart* part) { |
493 | 22.7k | if (part == nullptr) { |
494 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
495 | 0 | } |
496 | 22.7k | return curl_mime_data_cb(part, static_cast<curl_off_t>(total_size_), &UploadScriptState::ReadCallback, |
497 | 22.7k | &UploadScriptState::SeekCallback, nullptr, this); |
498 | 22.7k | } |
499 | | |
500 | | /// Install a protocol hook without exposing the mutable cursor or curl's |
501 | | /// retained callback userdata outside its owner. |
502 | 5.90k | void UploadScriptState::SetBeforeReadCallback(BeforeReadCallback callback, void* userdata) { |
503 | 5.90k | before_read_callback_ = callback; |
504 | 5.90k | before_read_userdata_ = userdata; |
505 | 5.90k | } |
506 | | |
507 | | /// Copy the bounded trailer prefix into callback-owned state. The callback is |
508 | | /// enabled only for HTTP schemes, which prevents compatibility inputs from |
509 | | /// attaching an HTTP callback to another protocol. |
510 | | /// @param scenario Complete scenario that owns the selected trailer message. |
511 | | HttpUploadTrailerState::HttpUploadTrailerState(const curl::fuzzer::proto::Scenario& scenario) |
512 | 491k | : line_count_(0), |
513 | 491k | callback_count_(0), |
514 | 491k | result_(curl::fuzzer::proto::HTTP_UPLOAD_TRAILER_SUCCESS), |
515 | 491k | enabled_(scenario.has_http_upload_trailers() && SupportsHttpUploadTrailers(scenario)) { |
516 | 491k | if (!enabled_) { |
517 | 482k | return; |
518 | 482k | } |
519 | | |
520 | 8.99k | const auto& source = scenario.http_upload_trailers(); |
521 | 8.99k | if (source.result() == curl::fuzzer::proto::HTTP_UPLOAD_TRAILER_ABORT) { |
522 | 1.31k | result_ = source.result(); |
523 | 1.31k | } |
524 | 8.99k | line_count_ = std::min<std::size_t>(scenario_limits::kMaxHttpUploadTrailers, source.lines_size()); |
525 | 12.5k | for (std::size_t index = 0; index < line_count_; ++index) { |
526 | 3.54k | const std::string& line = source.lines(static_cast<int>(index)); |
527 | 3.54k | lines_[index].assign(line.data(), std::min(line.size(), scenario_limits::kMaxHttpUploadTrailerBytes)); |
528 | 3.54k | } |
529 | 8.99k | } |
530 | | |
531 | | /// Build one fresh trailer list for libcurl. The caller takes ownership on a |
532 | | /// successful return. Allocation failure aborts the callback and frees any |
533 | | /// partial list because libcurl owns lists only after success. |
534 | | /// @param trailers Destination for the newly allocated list. |
535 | | /// @return CURL_TRAILERFUNC_OK or CURL_TRAILERFUNC_ABORT. |
536 | 3.72k | int HttpUploadTrailerState::BuildList(curl_slist** trailers) { |
537 | 3.72k | ++callback_count_; |
538 | 3.72k | if (trailers == nullptr) { |
539 | 0 | return CURL_TRAILERFUNC_ABORT; |
540 | 0 | } |
541 | 3.72k | *trailers = nullptr; |
542 | 3.72k | if (!enabled_) { |
543 | 0 | return CURL_TRAILERFUNC_OK; |
544 | 0 | } |
545 | 3.72k | if (result_ == curl::fuzzer::proto::HTTP_UPLOAD_TRAILER_ABORT) { |
546 | 45 | return CURL_TRAILERFUNC_ABORT; |
547 | 45 | } |
548 | | |
549 | 9.98k | for (std::size_t index = 0; index < line_count_; ++index) { |
550 | 6.31k | curl_slist* appended = curl_slist_append(*trailers, lines_[index].c_str()); |
551 | 6.31k | if (appended == nullptr) { |
552 | 11 | curl_slist_free_all(*trailers); |
553 | 11 | *trailers = nullptr; |
554 | 11 | return CURL_TRAILERFUNC_ABORT; |
555 | 11 | } |
556 | 6.30k | *trailers = appended; |
557 | 6.30k | } |
558 | 3.67k | return CURL_TRAILERFUNC_OK; |
559 | 3.68k | } |
560 | | |
561 | | /// C-compatible callback retained by libcurl for the complete transfer. |
562 | 3.72k | int HttpUploadTrailerState::TrailerCallback(curl_slist** trailers, void* userdata) { |
563 | 3.72k | if (userdata == nullptr) { |
564 | 0 | if (trailers != nullptr) { |
565 | 0 | *trailers = nullptr; |
566 | 0 | } |
567 | 0 | return CURL_TRAILERFUNC_ABORT; |
568 | 0 | } |
569 | 3.72k | return static_cast<HttpUploadTrailerState*>(userdata)->BuildList(trailers); |
570 | 3.72k | } |
571 | | |
572 | | /// @return True when a present trailer message belongs to an HTTP scheme. |
573 | 491k | bool HttpUploadTrailerState::enabled() const { return enabled_; } |
574 | | |
575 | | /// @return Number of bounded trailer lines copied from the scenario. |
576 | 8.99k | std::size_t HttpUploadTrailerState::line_count() const { return line_count_; } |
577 | | |
578 | | /// @return Number of callback list-build attempts. |
579 | 0 | std::size_t HttpUploadTrailerState::callback_count() const { return callback_count_; } |
580 | | |
581 | | /// Construct and apply the protocol-specific pointer-valued fields in `scenario` |
582 | | /// to `easy`. Setup errors are deliberately non-fatal: malformed or partially |
583 | | /// allocated scenarios should still exercise whatever curl state was built. |
584 | | /// `easy` must remain alive until this object is destroyed, because cleanup |
585 | | /// first detaches the pointers from the handle and only then frees them. |
586 | | /// @param easy Easy handle that will perform this scenario. |
587 | | /// @param scenario Source protocol lists, optional MIME body, and upload |
588 | | /// script. |
589 | | /// @param apply_resolve_entries Whether to install the scenario's bounded |
590 | | /// resolver mappings plus the mandatory loopback mapping. |
591 | | ScenarioRequestData::ScenarioRequestData(CURL* easy, const curl::fuzzer::proto::Scenario& scenario, |
592 | | bool apply_resolve_entries) |
593 | 491k | : easy_(easy), |
594 | 491k | request_headers_(nullptr), |
595 | 491k | resolve_entries_(nullptr), |
596 | 491k | telnet_options_(nullptr), |
597 | 491k | mail_rcpt_(nullptr), |
598 | 491k | ftp_quote_(nullptr), |
599 | 491k | ftp_prequote_(nullptr), |
600 | 491k | ftp_postquote_(nullptr), |
601 | 491k | upload_state_(scenario), |
602 | 491k | http_upload_trailer_state_(scenario), |
603 | 491k | mime_post_(nullptr), |
604 | 491k | upload_callbacks_installed_(false), |
605 | 491k | http_upload_trailer_callback_installed_(false), |
606 | 491k | resolve_entries_ready_(!apply_resolve_entries) { |
607 | 491k | if (easy_ == nullptr) { |
608 | 0 | return; |
609 | 0 | } |
610 | | |
611 | | // Complete all allocating work before installing retained pointers. If a |
612 | | // C++ allocation throws, scoped owners unwind without leaving the easy |
613 | | // handle pointing into a partially constructed request-data object. |
614 | 491k | bool resolve_entries_built = false; |
615 | 491k | if (apply_resolve_entries) { |
616 | 11.5k | resolve_entries_ = BuildStringList(scenario.resolve_entries(), scenario_limits::kMaxResolveEntries, |
617 | 11.5k | scenario_limits::kMaxResolveEntryBytes, &stats_.resolve_entries); |
618 | | // This mapping is deliberately last: preceding wildcard or removal |
619 | | // mutations may exercise host-cache parsing, but cannot redirect the |
620 | | // canonical resolver-lane origin away from the in-process mock. |
621 | 11.5k | curl_slist* appended = curl_slist_append(resolve_entries_.get(), "resolve.test:80:127.0.0.1"); |
622 | 11.5k | if (appended != nullptr) { |
623 | 11.5k | (void)resolve_entries_.release(); |
624 | 11.5k | resolve_entries_.reset(appended); |
625 | 11.5k | resolve_entries_built = true; |
626 | 11.5k | } |
627 | 11.5k | } |
628 | | |
629 | | // HTTP headers/MIME and TELNET options are mutually exclusive because only |
630 | | // the selected protocol can observe them. Avoid allocating protocol-inert |
631 | | // lists and trees in compatibility inputs that bypass target policy. |
632 | 491k | if (scenario.scheme() == curl::fuzzer::proto::SCHEME_TELNET) { |
633 | 5.90k | telnet_options_ = BuildStringList(scenario.telnet_options(), scenario_limits::kMaxTelnetOptions, |
634 | 5.90k | scenario_limits::kMaxTelnetOptionBytes, &stats_.telnet_options); |
635 | 485k | } else { |
636 | 485k | request_headers_ = BuildStringList(scenario.request_headers(), scenario_limits::kMaxRequestHeaders, |
637 | 485k | scenario_limits::kMaxMetadataBytes, &stats_.request_headers); |
638 | 485k | if (scenario.has_mime_post()) { |
639 | 81.7k | mime_post_ = BuildMimePost(easy_, scenario.mime_post(), &upload_state_, &stats_); |
640 | 81.7k | } |
641 | 485k | } |
642 | | |
643 | 491k | if (scenario.scheme() == curl::fuzzer::proto::SCHEME_SMTP || scenario.scheme() == curl::fuzzer::proto::SCHEME_SMTPS || |
644 | 484k | !scenario.mail_rcpt().empty()) { |
645 | | // Recipient list entries borrow protobuf-owned storage only for the |
646 | | // duration of BuildStringList; curl_slist_append copies each address. |
647 | 7.67k | mail_rcpt_ = BuildStringList(scenario.mail_rcpt(), scenario_limits::kMaxMailRecipients, |
648 | 7.67k | scenario_limits::kMaxMailRecipientBytes, &stats_.mail_rcpt); |
649 | 7.67k | } |
650 | | |
651 | 491k | if (scenario.scheme() == curl::fuzzer::proto::SCHEME_FTP || scenario.scheme() == curl::fuzzer::proto::SCHEME_FTPS) { |
652 | | // The three lists share a work budget in their execution order. Runtime |
653 | | // enforces the bound independently because compatibility inputs bypass |
654 | | // fixed-target postprocessing. |
655 | 9.96k | std::size_t remaining = scenario_limits::kMaxFtpQuoteCommands; |
656 | 9.96k | ftp_quote_ = |
657 | 9.96k | BuildStringList(scenario.ftp_quote(), remaining, scenario_limits::kMaxFtpQuoteCommandBytes, &stats_.ftp_quote); |
658 | 9.96k | remaining -= stats_.ftp_quote; |
659 | 9.96k | ftp_prequote_ = BuildStringList(scenario.ftp_prequote(), remaining, scenario_limits::kMaxFtpQuoteCommandBytes, |
660 | 9.96k | &stats_.ftp_prequote); |
661 | 9.96k | remaining -= stats_.ftp_prequote; |
662 | 9.96k | ftp_postquote_ = BuildStringList(scenario.ftp_postquote(), remaining, scenario_limits::kMaxFtpQuoteCommandBytes, |
663 | 9.96k | &stats_.ftp_postquote); |
664 | 9.96k | } |
665 | | |
666 | 491k | if (resolve_entries_built) { |
667 | 11.5k | resolve_entries_ready_ = curl_easy_setopt(easy_, CURLOPT_RESOLVE, resolve_entries_.get()) == CURLE_OK; |
668 | 11.5k | } |
669 | | |
670 | 491k | if (NeedsUploadCallbacks(scenario)) { |
671 | | // Install a per-run memory source even when Scenario.upload is absent but |
672 | | // CURLOPT_UPLOAD or TELNET may request caller input. Non-TELNET schemes |
673 | | // retain the historical fallback bytes; TELNET returns EOF. Either result |
674 | | // replaces stdin and cannot block OSS-Fuzz. The state remains scoped to |
675 | | // the complete drive so retries cannot share a cursor across iterations. |
676 | 59.0k | upload_callbacks_installed_ = true; |
677 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_READFUNCTION, &UploadScriptState::ReadCallback); |
678 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_READDATA, &upload_state_); |
679 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_SEEKFUNCTION, &UploadScriptState::SeekCallback); |
680 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_SEEKDATA, &upload_state_); |
681 | 59.0k | } |
682 | | |
683 | 491k | if (http_upload_trailer_state_.enabled()) { |
684 | 8.99k | stats_.http_upload_trailers = http_upload_trailer_state_.line_count(); |
685 | 8.99k | const CURLcode function_result = |
686 | 8.99k | curl_easy_setopt(easy_, CURLOPT_TRAILERFUNCTION, &HttpUploadTrailerState::TrailerCallback); |
687 | 8.99k | const CURLcode data_result = curl_easy_setopt(easy_, CURLOPT_TRAILERDATA, &http_upload_trailer_state_); |
688 | 8.99k | http_upload_trailer_callback_installed_ = function_result == CURLE_OK && data_result == CURLE_OK; |
689 | 8.99k | if (!http_upload_trailer_callback_installed_) { |
690 | 0 | (void)curl_easy_setopt(easy_, CURLOPT_TRAILERFUNCTION, nullptr); |
691 | 0 | (void)curl_easy_setopt(easy_, CURLOPT_TRAILERDATA, nullptr); |
692 | 0 | } |
693 | 8.99k | } |
694 | | |
695 | 491k | if (telnet_options_ != nullptr) { |
696 | 498 | (void)curl_easy_setopt(easy_, CURLOPT_TELNETOPTIONS, telnet_options_.get()); |
697 | 498 | } |
698 | 491k | if (mail_rcpt_ != nullptr) { |
699 | 3.83k | (void)curl_easy_setopt(easy_, CURLOPT_MAIL_RCPT, mail_rcpt_.get()); |
700 | 3.83k | } |
701 | 491k | if (ftp_quote_ != nullptr) { |
702 | 595 | (void)curl_easy_setopt(easy_, CURLOPT_QUOTE, ftp_quote_.get()); |
703 | 595 | } |
704 | 491k | if (ftp_prequote_ != nullptr) { |
705 | 450 | (void)curl_easy_setopt(easy_, CURLOPT_PREQUOTE, ftp_prequote_.get()); |
706 | 450 | } |
707 | 491k | if (ftp_postquote_ != nullptr) { |
708 | 632 | (void)curl_easy_setopt(easy_, CURLOPT_POSTQUOTE, ftp_postquote_.get()); |
709 | 632 | } |
710 | 491k | if (request_headers_ != nullptr) { |
711 | 73.0k | (void)curl_easy_setopt(easy_, CURLOPT_HTTPHEADER, request_headers_.get()); |
712 | 73.0k | } |
713 | 491k | if (mime_post_ != nullptr) { |
714 | 81.6k | (void)curl_easy_setopt(easy_, CURLOPT_MIMEPOST, mime_post_.get()); |
715 | 81.6k | } |
716 | 491k | } |
717 | | |
718 | | /// Detach pointer options/callbacks while the easy handle is valid, then release |
719 | | /// request allocations. libcurl does not copy headers, MIME roots, or callback |
720 | | /// userdata, so releasing any one before the mock drive ends would create a |
721 | | /// use-after-free; relying on easy cleanup to own header/MIME allocations would |
722 | | /// instead leak iterations. |
723 | 491k | ScenarioRequestData::~ScenarioRequestData() { |
724 | 491k | if (easy_ != nullptr) { |
725 | | // Clear callbacks before their userdata member is destroyed. There is no |
726 | | // perform in this destructor, but making the handle non-dangling keeps the |
727 | | // ownership rule robust if cleanup later gains diagnostics or getinfo. |
728 | 491k | if (upload_callbacks_installed_) { |
729 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_SEEKFUNCTION, nullptr); |
730 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_SEEKDATA, nullptr); |
731 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_READFUNCTION, nullptr); |
732 | 59.0k | (void)curl_easy_setopt(easy_, CURLOPT_READDATA, nullptr); |
733 | 59.0k | } |
734 | 491k | if (http_upload_trailer_callback_installed_) { |
735 | 8.99k | (void)curl_easy_setopt(easy_, CURLOPT_TRAILERFUNCTION, nullptr); |
736 | 8.99k | (void)curl_easy_setopt(easy_, CURLOPT_TRAILERDATA, nullptr); |
737 | 8.99k | } |
738 | 491k | if (mime_post_ != nullptr) { |
739 | 81.6k | (void)curl_easy_setopt(easy_, CURLOPT_MIMEPOST, nullptr); |
740 | 81.6k | } |
741 | 491k | if (request_headers_ != nullptr) { |
742 | 73.0k | (void)curl_easy_setopt(easy_, CURLOPT_HTTPHEADER, nullptr); |
743 | 73.0k | } |
744 | 491k | if (resolve_entries_ != nullptr) { |
745 | 11.5k | (void)curl_easy_setopt(easy_, CURLOPT_RESOLVE, nullptr); |
746 | 11.5k | } |
747 | 491k | if (telnet_options_ != nullptr) { |
748 | 498 | (void)curl_easy_setopt(easy_, CURLOPT_TELNETOPTIONS, nullptr); |
749 | 498 | } |
750 | 491k | if (mail_rcpt_ != nullptr) { |
751 | 3.83k | (void)curl_easy_setopt(easy_, CURLOPT_MAIL_RCPT, nullptr); |
752 | 3.83k | } |
753 | 491k | if (ftp_postquote_ != nullptr) { |
754 | 632 | (void)curl_easy_setopt(easy_, CURLOPT_POSTQUOTE, nullptr); |
755 | 632 | } |
756 | 491k | if (ftp_prequote_ != nullptr) { |
757 | 450 | (void)curl_easy_setopt(easy_, CURLOPT_PREQUOTE, nullptr); |
758 | 450 | } |
759 | 491k | if (ftp_quote_ != nullptr) { |
760 | 595 | (void)curl_easy_setopt(easy_, CURLOPT_QUOTE, nullptr); |
761 | 595 | } |
762 | 491k | } |
763 | | // Callback MIME parts retain upload_state_ as userdata. Release the root |
764 | | // explicitly while that state is still live instead of relying only on |
765 | | // reverse member destruction order. |
766 | 491k | mime_post_.reset(); |
767 | 491k | } |
768 | | |
769 | | /// Return resource counts after caps and allocation failures were applied, |
770 | | /// without exposing or transferring the owned curl pointers themselves. |
771 | | /// @return Immutable construction statistics. |
772 | 354k | const RequestBuildStats& ScenarioRequestData::stats() const { return stats_; } |
773 | | |
774 | | /// Return the callback state retained for the complete drive. This is mainly |
775 | | /// useful to ownership tests; the runner itself lets libcurl mutate it. |
776 | | /// @return Immutable upload state. |
777 | 354k | const UploadScriptState& ScenarioRequestData::upload_state() const { return upload_state_; } |
778 | | |
779 | | /// Return the owned request-trailer callback state for focused tests. |
780 | | /// @return Immutable trailer state. |
781 | 0 | const HttpUploadTrailerState& ScenarioRequestData::http_upload_trailer_state() const { |
782 | 0 | return http_upload_trailer_state_; |
783 | 0 | } |
784 | | |
785 | | /// Return whether this scenario needed raw upload callbacks. Exposing the |
786 | | /// decision lets ownership tests ensure ordinary requests avoid redundant |
787 | | /// setopt/teardown work without exposing either userdata pointer. |
788 | | /// @return True when read and seek callbacks were attached to the handle. |
789 | 354k | bool ScenarioRequestData::upload_callbacks_installed() const { return upload_callbacks_installed_; } |
790 | | |
791 | | /// @return True when request-trailer callbacks were attached to the handle. |
792 | 0 | bool ScenarioRequestData::http_upload_trailer_callback_installed() const { |
793 | 0 | return http_upload_trailer_callback_installed_; |
794 | 0 | } |
795 | | |
796 | | /// Return whether resolver-only slist construction installed its mandatory |
797 | | /// final loopback mapping. Ordinary lanes always report true. |
798 | | /// @return True when the resolver mapping is ready for the transfer. |
799 | 14.1k | bool ScenarioRequestData::resolve_entries_ready() const { return resolve_entries_ready_; } |
800 | | |
801 | | /// Arrange protocol-specific work immediately before every upload read. |
802 | | /// This is intentionally a narrow callback seam rather than exposing curl's |
803 | | /// retained READFUNCTION userdata or the mutable upload cursor. |
804 | | /// @param callback Function to invoke, or nullptr to clear the hook. |
805 | | /// @param userdata Borrowed pointer passed to `callback`. |
806 | 5.90k | void ScenarioRequestData::SetBeforeUploadReadCallback(UploadScriptState::BeforeReadCallback callback, void* userdata) { |
807 | 5.90k | upload_state_.SetBeforeReadCallback(callback, userdata); |
808 | 5.90k | } |
809 | | |
810 | | } // namespace proto_fuzzer |