merge bitcoin#26497: Make ConsumeNetAddr always produce valid onion addresses

This commit is contained in:
Kittywhiskers Van Gogh 2024-10-25 20:18:02 +00:00
parent 6a37934af4
commit 291305b4d2
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
12 changed files with 63 additions and 28 deletions

View File

@ -11,7 +11,8 @@ TEST_FUZZ_H = \
test/fuzz/fuzz.h \ test/fuzz/fuzz.h \
test/fuzz/FuzzedDataProvider.h \ test/fuzz/FuzzedDataProvider.h \
test/fuzz/util.h \ test/fuzz/util.h \
test/util/mining.h test/util/mining.h \
test/fuzz/util/net.h
libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS) libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
libtest_fuzz_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libtest_fuzz_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
@ -19,6 +20,7 @@ libtest_fuzz_a_SOURCES = \
test/fuzz/fuzz.cpp \ test/fuzz/fuzz.cpp \
test/util/mining.cpp \ test/util/mining.cpp \
test/fuzz/util.cpp \ test/fuzz/util.cpp \
test/fuzz/util/net.cpp \
$(TEST_FUZZ_H) $(TEST_FUZZ_H)
LIBTEST_FUZZ += $(LIBBITCOIN_SERVER) LIBTEST_FUZZ += $(LIBBITCOIN_SERVER)

View File

@ -598,7 +598,7 @@ static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
return r; return r;
} }
static std::string OnionToString(Span<const uint8_t> addr) std::string OnionToString(Span<const uint8_t> addr)
{ {
uint8_t checksum[torv3::CHECKSUM_LEN]; uint8_t checksum[torv3::CHECKSUM_LEN];
torv3::Checksum(addr, checksum); torv3::Checksum(addr, checksum);

View File

@ -113,6 +113,8 @@ static constexpr size_t ADDR_INTERNAL_SIZE = 10;
/// SAM 3.1 and earlier do not support specifying ports and force the port to 0. /// SAM 3.1 and earlier do not support specifying ports and force the port to 0.
static constexpr uint16_t I2P_SAM31_PORT{0}; static constexpr uint16_t I2P_SAM31_PORT{0};
std::string OnionToString(Span<const uint8_t> addr);
/** /**
* Network address. * Network address.
*/ */

View File

@ -11,6 +11,7 @@
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h> #include <test/fuzz/util.h>
#include <test/fuzz/util/net.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <time.h> #include <time.h>
#include <util/asmap.h> #include <util/asmap.h>

View File

@ -8,6 +8,7 @@
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h> #include <test/fuzz/util.h>
#include <test/fuzz/util/net.h>
#include <util/readwritefile.h> #include <util/readwritefile.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <util/system.h> #include <util/system.h>

View File

@ -11,6 +11,7 @@
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h> #include <test/fuzz/util.h>
#include <test/fuzz/util/net.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <util/system.h> #include <util/system.h>
#include <util/translation.h> #include <util/translation.h>

View File

@ -5,7 +5,7 @@
#include <netaddress.h> #include <netaddress.h>
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h> #include <test/fuzz/util/net.h>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>

View File

@ -6,7 +6,7 @@
#include <netbase.h> #include <netbase.h>
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h> #include <test/fuzz/util/net.h>
#include <cstdint> #include <cstdint>
#include <string> #include <string>

View File

@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <net_processing.h> #include <net_processing.h>
#include <netaddress.h>
#include <netmessagemaker.h> #include <netmessagemaker.h>
#include <test/fuzz/util.h> #include <test/fuzz/util.h>
#include <test/util/script.h> #include <test/util/script.h>
@ -389,28 +390,6 @@ bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) n
return false; return false;
} }
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept
{
const Network network = fuzzed_data_provider.PickValueInArray({Network::NET_IPV4, Network::NET_IPV6, Network::NET_INTERNAL, Network::NET_ONION});
CNetAddr net_addr;
if (network == Network::NET_IPV4) {
in_addr v4_addr = {};
v4_addr.s_addr = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
net_addr = CNetAddr{v4_addr};
} else if (network == Network::NET_IPV6) {
if (fuzzed_data_provider.remaining_bytes() >= 16) {
in6_addr v6_addr = {};
memcpy(v6_addr.s6_addr, fuzzed_data_provider.ConsumeBytes<uint8_t>(16).data(), 16);
net_addr = CNetAddr{v6_addr, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
}
} else if (network == Network::NET_INTERNAL) {
net_addr.SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
} else if (network == Network::NET_ONION) {
net_addr.SetSpecial(fuzzed_data_provider.ConsumeBytesAsString(32));
}
return net_addr;
}
FILE* FuzzedFileProvider::open() FILE* FuzzedFileProvider::open()
{ {
SetFuzzedErrNo(m_fuzzed_data_provider); SetFuzzedErrNo(m_fuzzed_data_provider);

View File

@ -24,6 +24,7 @@
#include <streams.h> #include <streams.h>
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util/net.h>
#include <test/util/net.h> #include <test/util/net.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <txmempool.h> #include <txmempool.h>
@ -296,8 +297,6 @@ template<typename B = uint8_t>
return random_bytes; return random_bytes;
} }
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept;
inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
{ {
return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()}; return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};

View File

@ -0,0 +1,36 @@
// Copyright (c) 2009-2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <compat.h>
#include <netaddress.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <util/strencodings.h>
#include <cstdint>
#include <vector>
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept
{
const Network network = fuzzed_data_provider.PickValueInArray({Network::NET_IPV4, Network::NET_IPV6, Network::NET_INTERNAL, Network::NET_ONION});
CNetAddr net_addr;
if (network == Network::NET_IPV4) {
in_addr v4_addr = {};
v4_addr.s_addr = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
net_addr = CNetAddr{v4_addr};
} else if (network == Network::NET_IPV6) {
if (fuzzed_data_provider.remaining_bytes() >= 16) {
in6_addr v6_addr = {};
memcpy(v6_addr.s6_addr, fuzzed_data_provider.ConsumeBytes<uint8_t>(16).data(), 16);
net_addr = CNetAddr{v6_addr, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
}
} else if (network == Network::NET_INTERNAL) {
net_addr.SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
} else if (network == Network::NET_ONION) {
auto pub_key{fuzzed_data_provider.ConsumeBytes<uint8_t>(ADDR_TORV3_SIZE)};
pub_key.resize(ADDR_TORV3_SIZE);
const bool ok{net_addr.SetSpecial(OnionToString(pub_key))};
assert(ok);
}
return net_addr;
}

14
src/test/fuzz/util/net.h Normal file
View File

@ -0,0 +1,14 @@
// Copyright (c) 2009-2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_FUZZ_UTIL_NET_H
#define BITCOIN_TEST_FUZZ_UTIL_NET_H
#include <netaddress.h>
class FuzzedDataProvider;
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept;
#endif // BITCOIN_TEST_FUZZ_UTIL_NET_H