Skip to content

Commit fa454dc

Browse files
author
MarcoFalke
committed
net: Use steady clock in InterruptibleRecv
1 parent 5150e28 commit fa454dc

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/netbase.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ static Proxy nameProxy GUARDED_BY(g_proxyinfo_mutex);
3636
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
3737
bool fNameLookup = DEFAULT_NAME_LOOKUP;
3838

39-
// Need ample time for negotiation for very slow proxies such as Tor (milliseconds)
40-
int g_socks5_recv_timeout = 20 * 1000;
39+
// Need ample time for negotiation for very slow proxies such as Tor
40+
std::chrono::milliseconds g_socks5_recv_timeout = 20s;
4141
static std::atomic<bool> interruptSocks5Recv(false);
4242

4343
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
@@ -296,7 +296,7 @@ enum class IntrRecvError {
296296
*
297297
* @param data The buffer where the read bytes should be stored.
298298
* @param len The number of bytes to read into the specified buffer.
299-
* @param timeout The total timeout in milliseconds for this read.
299+
* @param timeout The total timeout for this read.
300300
* @param sock The socket (has to be in non-blocking mode) from which to read bytes.
301301
*
302302
* @returns An IntrRecvError indicating the resulting status of this read.
@@ -306,10 +306,10 @@ enum class IntrRecvError {
306306
* @see This function can be interrupted by calling InterruptSocks5(bool).
307307
* Sockets can be made non-blocking with Sock::SetNonBlocking().
308308
*/
309-
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& sock)
309+
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, std::chrono::milliseconds timeout, const Sock& sock)
310310
{
311-
int64_t curTime = GetTimeMillis();
312-
int64_t endTime = curTime + timeout;
311+
auto curTime{Now<SteadyMilliseconds>()};
312+
const auto endTime{curTime + timeout};
313313
while (len > 0 && curTime < endTime) {
314314
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
315315
if (ret > 0) {
@@ -333,7 +333,7 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
333333
}
334334
if (interruptSocks5Recv)
335335
return IntrRecvError::Interrupted;
336-
curTime = GetTimeMillis();
336+
curTime = Now<SteadyMilliseconds>();
337337
}
338338
return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
339339
}

src/test/fuzz/socks5.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#include <string>
1515
#include <vector>
1616

17+
extern std::chrono::milliseconds g_socks5_recv_timeout;
18+
1719
namespace {
18-
int default_socks5_recv_timeout;
20+
decltype(g_socks5_recv_timeout) default_socks5_recv_timeout;
1921
};
2022

21-
extern int g_socks5_recv_timeout;
22-
2323
void initialize_socks5()
2424
{
2525
static const auto testing_setup = MakeNoLogFileContext<const BasicTestingSetup>();
@@ -35,7 +35,7 @@ FUZZ_TARGET_INIT(socks5, initialize_socks5)
3535
InterruptSocks5(fuzzed_data_provider.ConsumeBool());
3636
// Set FUZZED_SOCKET_FAKE_LATENCY=1 to exercise recv timeout code paths. This
3737
// will slow down fuzzing.
38-
g_socks5_recv_timeout = (fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) ? 1 : default_socks5_recv_timeout;
38+
g_socks5_recv_timeout = (fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) ? 1ms : default_socks5_recv_timeout;
3939
FuzzedSock fuzzed_sock = ConsumeSock(fuzzed_data_provider);
4040
// This Socks5(...) fuzzing harness would have caught CVE-2017-18350 within
4141
// a few seconds of fuzzing.

0 commit comments

Comments
 (0)