/src/curl_fuzzer/proto_fuzzer/transfer_session.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 Ordered easy-handle and retained-option resource ownership. |
9 | | |
10 | | #include "proto_fuzzer/transfer_session.h" |
11 | | |
12 | | #include <algorithm> |
13 | | #include <cstddef> |
14 | | #include <cstdint> |
15 | | #include <utility> |
16 | | |
17 | | #include "proto_fuzzer/api_lifecycle.h" |
18 | | #include "proto_fuzzer/bounded_anonymous_input_file.h" |
19 | | #include "proto_fuzzer/option_apply.h" |
20 | | #include "proto_fuzzer/request_data.h" |
21 | | #include "proto_fuzzer/scenario_limits.h" |
22 | | #include "proto_fuzzer/tls_test_credentials.h" |
23 | | |
24 | | namespace proto_fuzzer { |
25 | | namespace { |
26 | | |
27 | | constexpr char kDevNullPath[] = "/dev/null"; |
28 | | constexpr char kAltSvcOrigin[] = "altsvc-origin.test"; |
29 | | constexpr char kAltSvcLoopbackResolve[] = "*:80:127.0.1.127"; |
30 | | |
31 | | /// Fail closed before any resolver backend starts ambient DNS work. |
32 | 12 | int AbortUnexpectedAltSvcResolve(void*, void*, void*) { return 1; } |
33 | | |
34 | 631 | bool HasCanonicalAltSvcAuthority(const std::string& host_path) { |
35 | 631 | const std::size_t host_size = sizeof(kAltSvcOrigin) - 1; |
36 | 631 | return host_path.compare(0, host_size, kAltSvcOrigin) == 0 && |
37 | 631 | (host_path.size() == host_size || host_path[host_size] == '/' || host_path[host_size] == '?' || |
38 | 9 | host_path[host_size] == '#'); |
39 | 631 | } |
40 | | |
41 | | /// Repeat fixed-target bounds for direct runtime and compatibility callers. |
42 | | void PrepareInputFile(const std::string& contents, std::size_t* remaining_bytes, BoundedAnonymousInputFile* input_file, |
43 | 295k | bool guard_netrc = false) { |
44 | 295k | const std::size_t size = std::min(contents.size(), std::min(scenario_limits::kMaxFileInputBytes, *remaining_bytes)); |
45 | 295k | if (size == 0) { |
46 | 273k | return; |
47 | 273k | } |
48 | 21.7k | if (guard_netrc) { |
49 | | // Retain a parser-neutral newline when curl's loader drops comment lines. |
50 | | // Charge only protobuf bytes to the shared budget, as before. |
51 | 5.61k | std::string guarded_contents(1, '\n'); |
52 | 5.61k | guarded_contents.append(contents.data(), size); |
53 | 5.61k | if (input_file->Write(reinterpret_cast<const std::uint8_t*>(guarded_contents.data()), guarded_contents.size())) { |
54 | 5.61k | *remaining_bytes -= size; |
55 | 5.61k | } |
56 | 16.1k | } else if (input_file->Write(reinterpret_cast<const std::uint8_t*>(contents.data()), size)) { |
57 | 16.1k | *remaining_bytes -= size; |
58 | 16.1k | } |
59 | 21.7k | } |
60 | | |
61 | | } // namespace |
62 | | |
63 | | /// Anonymous files are allocated only for file-backed lanes and survive easy |
64 | | /// cleanup. |
65 | | struct TransferSession::ParserInputs { |
66 | | BoundedAnonymousInputFile file_source{scenario_limits::kMaxFileInputBytes}; |
67 | | BoundedAnonymousInputFile cookie{scenario_limits::kMaxFileInputBytes}; |
68 | | BoundedAnonymousInputFile altsvc{scenario_limits::kMaxFileInputBytes}; |
69 | | BoundedAnonymousInputFile hsts{scenario_limits::kMaxFileInputBytes}; |
70 | | BoundedAnonymousInputFile netrc{scenario_limits::kMaxFileInputBytes + 1}; |
71 | | BoundedAnonymousInputFile crl{scenario_limits::kMaxFileInputBytes}; |
72 | | BoundedAnonymousInputFile pinned_public_key{scenario_limits::kMaxFileInputBytes}; |
73 | | BoundedAnonymousInputFile client_certificate{sizeof(tls_test_credentials::kCertificatePem) - 1}; |
74 | | BoundedAnonymousInputFile client_private_key{sizeof(tls_test_credentials::kPrivateKeyPem) - 1}; |
75 | | }; |
76 | | |
77 | 517k | TransferSession::TransferSession() = default; |
78 | 517k | TransferSession::~TransferSession() { Close(); } |
79 | | |
80 | | /// Allocate the easy lazily so unused concurrent-transfer slots stay empty. |
81 | | /// @return Whether the session has a usable easy handle. |
82 | 492k | bool TransferSession::Initialize() { |
83 | 492k | if (!easy_) { |
84 | 492k | easy_.reset(curl_easy_init()); |
85 | 492k | } |
86 | 492k | return easy_ != nullptr; |
87 | 492k | } |
88 | | |
89 | | /// Borrow the owned easy; callers must not clean it themselves. |
90 | | /// @return Easy handle, or nullptr before successful initialization. |
91 | 1.30M | CURL* TransferSession::easy() const { return easy_.get(); } |
92 | | |
93 | | /// Apply baseline options and retain their CONNECT_TO storage. |
94 | | /// @param scenario Transfer whose scheme, trace_ids, and trace_config select the |
95 | | /// peer allowlist and trace configuration. |
96 | 492k | void TransferSession::ApplyBaseline(const curl::fuzzer::proto::Scenario& scenario) { |
97 | 492k | if (easy_) { |
98 | | // Apply the new pointer before releasing the previous list. |
99 | 492k | connect_to_ = ApplyBaselineOptions(easy_.get(), scenario.scheme(), scenario.trace_ids(), scenario.trace_config()); |
100 | 492k | } |
101 | 492k | } |
102 | | |
103 | | /// Reset pre-transfer configuration while preserving prepared parser files. |
104 | | /// @return False if request or API callback state has already been installed. |
105 | 1.66k | bool TransferSession::ResetConfiguration() { |
106 | 1.66k | if (!easy_ || request_data_ || api_lifecycle_) { |
107 | 0 | return false; |
108 | 0 | } |
109 | 1.66k | curl_easy_reset(easy_.get()); |
110 | 1.66k | connect_to_.reset(); |
111 | 1.66k | altsvc_resolve_.reset(); |
112 | 1.66k | return true; |
113 | 1.66k | } |
114 | | |
115 | | /// Copy bounded parser inputs into lazily allocated anonymous files. |
116 | | /// @param scenario Source of parser-file bytes; not retained. |
117 | | /// @param mode Select the FILE source or deep HTTP/TLS parser inputs. |
118 | | /// @return False after easy initialization, when paths may be retained. |
119 | 413k | bool TransferSession::PrepareInputFiles(const curl::fuzzer::proto::Scenario& scenario, ScenarioRunMode mode) { |
120 | 413k | if (easy_) { |
121 | 0 | return false; |
122 | 0 | } |
123 | 413k | if (mode != ScenarioRunMode::kFileCoverage && mode != ScenarioRunMode::kDeepHttpCoverage && |
124 | 378k | mode != ScenarioRunMode::kTlsCoverage) { |
125 | 291k | return true; |
126 | 291k | } |
127 | 121k | parser_inputs_ = std::make_unique<ParserInputs>(); |
128 | 121k | if (mode == ScenarioRunMode::kFileCoverage) { |
129 | 4.05k | const std::size_t size = std::min(scenario.file_source().size(), scenario_limits::kMaxFileInputBytes); |
130 | 4.05k | return parser_inputs_->file_source.Write(reinterpret_cast<const std::uint8_t*>(scenario.file_source().data()), |
131 | 4.05k | size); |
132 | 4.05k | } |
133 | 117k | std::size_t remaining_bytes = scenario_limits::kMaxFileInputTotalBytes; |
134 | 117k | if (mode == ScenarioRunMode::kDeepHttpCoverage) { |
135 | 30.2k | PrepareInputFile(scenario.cookie_file(), &remaining_bytes, &parser_inputs_->cookie); |
136 | 30.2k | PrepareInputFile(scenario.altsvc_file(), &remaining_bytes, &parser_inputs_->altsvc); |
137 | 30.2k | PrepareInputFile(scenario.hsts_file(), &remaining_bytes, &parser_inputs_->hsts); |
138 | 30.2k | PrepareInputFile(scenario.netrc_file(), &remaining_bytes, &parser_inputs_->netrc, true); |
139 | 87.3k | } else { |
140 | 87.3k | PrepareInputFile(scenario.crl_file(), &remaining_bytes, &parser_inputs_->crl); |
141 | 87.3k | PrepareInputFile(scenario.pinned_public_key_file(), &remaining_bytes, &parser_inputs_->pinned_public_key); |
142 | 87.3k | if (scenario.tls_client_credential_profile() == curl::fuzzer::proto::TLS_CLIENT_CREDENTIAL_VALID_PEM && |
143 | 7.01k | TlsClientCredentialFilesRequired()) { |
144 | 1.53k | const bool certificate_ready = parser_inputs_->client_certificate.Write( |
145 | 1.53k | reinterpret_cast<const std::uint8_t*>(tls_test_credentials::kCertificatePem), |
146 | 1.53k | sizeof(tls_test_credentials::kCertificatePem) - 1); |
147 | 1.53k | const bool key_ready = parser_inputs_->client_private_key.Write( |
148 | 1.53k | reinterpret_cast<const std::uint8_t*>(tls_test_credentials::kPrivateKeyPem), |
149 | 1.53k | sizeof(tls_test_credentials::kPrivateKeyPem) - 1); |
150 | 1.53k | if (!certificate_ready || !key_ready) { |
151 | 0 | return false; |
152 | 0 | } |
153 | 1.53k | } |
154 | 87.3k | } |
155 | 117k | return true; |
156 | 117k | } |
157 | | |
158 | | /// Return the pathname of the prepared FILE source/sink. The path names only |
159 | | /// a descriptor owned by this session and is unavailable in every other lane. |
160 | | /// @return Stable `/proc/self/fd/N` path, or nullptr without a ready source. |
161 | 3.99k | const char* TransferSession::FileSourcePath() const { |
162 | 3.99k | return parser_inputs_ ? parser_inputs_->file_source.path() : nullptr; |
163 | 3.99k | } |
164 | | |
165 | | /// Apply prepared parser paths after baseline and peer installation. |
166 | 413k | void TransferSession::ApplyInputFiles() { |
167 | 413k | if (!easy_ || !parser_inputs_) { |
168 | 292k | return; |
169 | 292k | } |
170 | 121k | if (const char* path = parser_inputs_->cookie.path()) { |
171 | 6.35k | (void)curl_easy_setopt(easy_.get(), CURLOPT_COOKIEFILE, path); |
172 | 6.35k | } |
173 | | // Alt-Svc/HSTS use one option for input and cleanup output. Load first, |
174 | | // then restore the fixed sink; both loaders retain earlier file entries. |
175 | 121k | if (const char* path = parser_inputs_->altsvc.path()) { |
176 | 631 | (void)curl_easy_setopt(easy_.get(), CURLOPT_ALTSVC, path); |
177 | 631 | (void)curl_easy_setopt(easy_.get(), CURLOPT_ALTSVC, kDevNullPath); |
178 | 631 | } |
179 | 121k | if (const char* path = parser_inputs_->hsts.path()) { |
180 | 2.50k | (void)curl_easy_setopt(easy_.get(), CURLOPT_HSTS, path); |
181 | 2.50k | (void)curl_easy_setopt(easy_.get(), CURLOPT_HSTS, kDevNullPath); |
182 | 2.50k | } |
183 | 121k | if (const char* path = parser_inputs_->netrc.path()) { |
184 | 5.61k | (void)curl_easy_setopt(easy_.get(), CURLOPT_NETRC_FILE, path); |
185 | 5.61k | (void)curl_easy_setopt(easy_.get(), CURLOPT_NETRC, CURL_NETRC_REQUIRED); |
186 | 5.61k | } |
187 | 121k | if (const char* path = parser_inputs_->crl.path()) { |
188 | 3.94k | (void)curl_easy_setopt(easy_.get(), CURLOPT_CRLFILE, path); |
189 | 3.94k | } |
190 | 121k | } |
191 | | |
192 | | /// Install the prepared public-key pin path after generic scalar options. |
193 | | /// This field is the only protobuf-controlled route to curl's filename-backed |
194 | | /// pin parser; the path itself always names a descriptor owned by this session. |
195 | 413k | void TransferSession::ApplyPinnedPublicKeyFile() { |
196 | 413k | if (!easy_ || !parser_inputs_) { |
197 | 292k | return; |
198 | 292k | } |
199 | 121k | if (const char* path = parser_inputs_->pinned_public_key.path()) { |
200 | 2.72k | (void)curl_easy_setopt(easy_.get(), CURLOPT_PINNEDPUBLICKEY, path); |
201 | 2.72k | } |
202 | 121k | } |
203 | | |
204 | | /// Apply one closed client-credential profile with session-owned file paths |
205 | | /// available to backends that do not implement curl's certificate blob options. |
206 | | /// @param profile Fixed credential setup selected by the HTTPS lane. |
207 | | /// @return The first curl option error, or CURLE_OK. |
208 | 87.2k | CURLcode TransferSession::ApplyTlsClientCredentialProfile(curl::fuzzer::proto::TlsClientCredentialProfile profile) { |
209 | 87.2k | if (!easy_) { |
210 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
211 | 0 | } |
212 | 87.2k | const char* certificate_path = parser_inputs_ ? parser_inputs_->client_certificate.path() : nullptr; |
213 | 87.2k | const char* private_key_path = parser_inputs_ ? parser_inputs_->client_private_key.path() : nullptr; |
214 | 87.2k | return proto_fuzzer::ApplyTlsClientCredentialProfile(easy_.get(), profile, certificate_path, private_key_path); |
215 | 87.2k | } |
216 | | |
217 | | /// Enable isolated Alt-Svc routing for the canonical deep-HTTP authority. |
218 | | /// @param host_path Scenario URL authority/path used by this transfer. |
219 | 385k | void TransferSession::ConfigureAltSvcRouting(const std::string& host_path) { |
220 | 385k | if (!easy_ || !parser_inputs_ || parser_inputs_->altsvc.path() == nullptr || |
221 | 384k | !HasCanonicalAltSvcAuthority(host_path)) { |
222 | 384k | return; |
223 | 384k | } |
224 | 631 | CurlSlistPtr resolve(curl_slist_append(nullptr, kAltSvcLoopbackResolve)); |
225 | 631 | if (!resolve || curl_easy_setopt(easy_.get(), CURLOPT_RESOLVE, resolve.get()) != CURLE_OK) { |
226 | 2 | return; |
227 | 2 | } |
228 | | // Adopt only after installation; failures leave the previous list alive. |
229 | 629 | altsvc_resolve_ = std::move(resolve); |
230 | 629 | if (curl_easy_setopt(easy_.get(), CURLOPT_RESOLVER_START_FUNCTION, &AbortUnexpectedAltSvcResolve) == CURLE_OK) { |
231 | | // CONNECT_TO takes precedence over Alt-Svc. Only detach it when the |
232 | | // wildcard host cache and fail-closed resolver callback are installed. |
233 | 629 | (void)curl_easy_setopt(easy_.get(), CURLOPT_CONNECT_TO, nullptr); |
234 | 629 | } |
235 | 629 | } |
236 | | |
237 | | /// Install owned request attachments. The Scenario remains borrowed. |
238 | | /// @param scenario Live, unmodified source until session teardown. |
239 | | /// @param apply_resolve_entries Install resolver-lane mappings. |
240 | | /// @return Request owner, or nullptr without an easy or if already installed. |
241 | | ScenarioRequestData* TransferSession::InstallRequestData(const curl::fuzzer::proto::Scenario& scenario, |
242 | 491k | bool apply_resolve_entries) { |
243 | 491k | if (!easy_ || request_data_) { |
244 | 0 | return nullptr; |
245 | 0 | } |
246 | 491k | request_data_.emplace(easy_.get(), scenario, apply_resolve_entries); |
247 | 491k | return &*request_data_; |
248 | 491k | } |
249 | | |
250 | | /// Install API/share state that survives easy cleanup. |
251 | | /// @param plan Live plan until session teardown. |
252 | | /// @param url URL used only during lifecycle construction. |
253 | | /// @return Lifecycle owner, or nullptr if absent/already installed. |
254 | 7.88k | ApiLifecycle* TransferSession::InstallApiLifecycle(const curl::fuzzer::proto::ApiPlan& plan, std::string_view url) { |
255 | 7.88k | if (!easy_ || api_lifecycle_) { |
256 | 0 | return nullptr; |
257 | 0 | } |
258 | 7.88k | api_lifecycle_ = std::make_unique<ApiLifecycle>(easy_.get(), plan, url); |
259 | 7.88k | return api_lifecycle_.get(); |
260 | 7.88k | } |
261 | | |
262 | | /// Release all resources in their required order. Safe to call repeatedly. |
263 | 623k | void TransferSession::Close() { |
264 | 623k | request_data_.reset(); |
265 | 623k | easy_.reset(); |
266 | 623k | api_lifecycle_.reset(); |
267 | 623k | altsvc_resolve_.reset(); |
268 | 623k | connect_to_.reset(); |
269 | 623k | parser_inputs_.reset(); |
270 | 623k | } |
271 | | |
272 | | } // namespace proto_fuzzer |