Bitcoin Core  22.99.0
P2P Digital Currency
socks5.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <netaddress.h>
6 #include <netbase.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <test/util/setup_common.h>
11 
12 #include <cstdint>
13 #include <string>
14 #include <vector>
15 
16 namespace {
17 int default_socks5_recv_timeout;
18 };
19 
20 extern int g_socks5_recv_timeout;
21 
23 {
24  static const auto testing_setup = MakeNoLogFileContext<const BasicTestingSetup>();
25  default_socks5_recv_timeout = g_socks5_recv_timeout;
26 }
27 
29 {
30  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
31  ProxyCredentials proxy_credentials;
32  proxy_credentials.username = fuzzed_data_provider.ConsumeRandomLengthString(512);
33  proxy_credentials.password = fuzzed_data_provider.ConsumeRandomLengthString(512);
34  InterruptSocks5(fuzzed_data_provider.ConsumeBool());
35  // Set FUZZED_SOCKET_FAKE_LATENCY=1 to exercise recv timeout code paths. This
36  // will slow down fuzzing.
37  g_socks5_recv_timeout = (fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) ? 1 : default_socks5_recv_timeout;
38  FuzzedSock fuzzed_sock = ConsumeSock(fuzzed_data_provider);
39  // This Socks5(...) fuzzing harness would have caught CVE-2017-18350 within
40  // a few seconds of fuzzing.
41  (void)Socks5(fuzzed_data_provider.ConsumeRandomLengthString(512),
42  fuzzed_data_provider.ConsumeIntegral<uint16_t>(),
43  fuzzed_data_provider.ConsumeBool() ? &proxy_credentials : nullptr,
44  fuzzed_sock);
45 }
setup_common.h
FuzzedSock
Definition: util.h:387
ConsumeSock
FuzzedSock ConsumeSock(FuzzedDataProvider &fuzzed_data_provider)
Definition: util.h:420
util.h
Socks5
bool Socks5(const std::string &strDest, uint16_t port, const ProxyCredentials *auth, const Sock &sock)
Connect to a specified destination service through an already connected SOCKS5 proxy.
Definition: netbase.cpp:364
netaddress.h
FuzzedDataProvider.h
ProxyCredentials::password
std::string password
Definition: netbase.h:64
ProxyCredentials
Credentials for proxy authentication.
Definition: netbase.h:61
InterruptSocks5
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:751
initialize_socks5
void initialize_socks5()
Definition: socks5.cpp:22
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(socks5, initialize_socks5)
Definition: socks5.cpp:28
g_socks5_recv_timeout
int g_socks5_recv_timeout
Definition: netbase.cpp:40
netbase.h
ProxyCredentials::username
std::string username
Definition: netbase.h:63