Skip to content

Commit 0968c51

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#26497: fuzz: Make ConsumeNetAddr always produce valid onion addresses
0eeb9b0 [fuzz] Move ConsumeNetAddr to fuzz/util/net.h (dergoegge) 291c869 [fuzz] Make ConsumeNetAddr produce valid onion addresses (dergoegge) c9ba3f8 [netaddress] Make OnionToString public (dergoegge) Pull request description: The chance that the fuzzer is able to guess a valid onion address is probably slim, as they are Base32 encoded and include a checksum. Right now, any target using `ConsumeNetAddr` would have a hard time uncovering bugs that require valid onion addresses as input. This PR makes `ConsumeNetAddr` produce valid onion addresses by using the 32 bytes given by the fuzzer as the pubkey for the onion address and forming a valid address according to the torv3 spec. ACKs for top commit: vasild: ACK 0eeb9b0 brunoerg: ACK 0eeb9b0 Tree-SHA512: 7c687a4d12f9659559be8f0c3cd4265167d1261d419cfd3d503fd7c7f207cc0db745220f02fb1737e4a5700ea7429311cfc0b42e6c15968ce6a85f8813c7e1d8
2 parents df2f166 + 0eeb9b0 commit 0968c51

File tree

12 files changed

+63
-28
lines changed

12 files changed

+63
-28
lines changed

src/Makefile.test_fuzz.include

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ TEST_FUZZ_H = \
1111
test/fuzz/fuzz.h \
1212
test/fuzz/FuzzedDataProvider.h \
1313
test/fuzz/util.h \
14-
test/fuzz/util/mempool.h
14+
test/fuzz/util/mempool.h \
15+
test/fuzz/util/net.h
1516

1617
libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
1718
libtest_fuzz_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1819
libtest_fuzz_a_SOURCES = \
1920
test/fuzz/fuzz.cpp \
2021
test/fuzz/util.cpp \
2122
test/fuzz/util/mempool.cpp \
23+
test/fuzz/util/net.cpp \
2224
$(TEST_FUZZ_H)

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
588588
return r;
589589
}
590590

591-
static std::string OnionToString(Span<const uint8_t> addr)
591+
std::string OnionToString(Span<const uint8_t> addr)
592592
{
593593
uint8_t checksum[torv3::CHECKSUM_LEN];
594594
torv3::Checksum(addr, checksum);

src/netaddress.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ static constexpr size_t ADDR_INTERNAL_SIZE = 10;
111111
/// SAM 3.1 and earlier do not support specifying ports and force the port to 0.
112112
static constexpr uint16_t I2P_SAM31_PORT{0};
113113

114+
std::string OnionToString(Span<const uint8_t> addr);
115+
114116
/**
115117
* Network address.
116118
*/

src/test/fuzz/addrman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <test/fuzz/FuzzedDataProvider.h>
1212
#include <test/fuzz/fuzz.h>
1313
#include <test/fuzz/util.h>
14+
#include <test/fuzz/util/net.h>
1415
#include <test/util/setup_common.h>
1516
#include <time.h>
1617
#include <util/asmap.h>

src/test/fuzz/banman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <test/fuzz/FuzzedDataProvider.h>
99
#include <test/fuzz/fuzz.h>
1010
#include <test/fuzz/util.h>
11+
#include <test/fuzz/util/net.h>
1112
#include <test/util/setup_common.h>
1213
#include <util/readwritefile.h>
1314
#include <util/system.h>

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <test/fuzz/FuzzedDataProvider.h>
1212
#include <test/fuzz/fuzz.h>
1313
#include <test/fuzz/util.h>
14+
#include <test/fuzz/util/net.h>
1415
#include <test/util/setup_common.h>
1516
#include <util/system.h>
1617
#include <util/translation.h>

src/test/fuzz/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <netaddress.h>
66
#include <test/fuzz/FuzzedDataProvider.h>
77
#include <test/fuzz/fuzz.h>
8-
#include <test/fuzz/util.h>
8+
#include <test/fuzz/util/net.h>
99

1010
#include <cassert>
1111
#include <cstdint>

src/test/fuzz/netbase_dns_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <netbase.h>
77
#include <test/fuzz/FuzzedDataProvider.h>
88
#include <test/fuzz/fuzz.h>
9-
#include <test/fuzz/util.h>
9+
#include <test/fuzz/util/net.h>
1010

1111
#include <cstdint>
1212
#include <string>

src/test/fuzz/util.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <consensus/amount.h>
66
#include <net_processing.h>
7+
#include <netaddress.h>
78
#include <netmessagemaker.h>
89
#include <pubkey.h>
910
#include <test/fuzz/util.h>
@@ -507,28 +508,6 @@ bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) n
507508
return false;
508509
}
509510

510-
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept
511-
{
512-
const Network network = fuzzed_data_provider.PickValueInArray({Network::NET_IPV4, Network::NET_IPV6, Network::NET_INTERNAL, Network::NET_ONION});
513-
CNetAddr net_addr;
514-
if (network == Network::NET_IPV4) {
515-
in_addr v4_addr = {};
516-
v4_addr.s_addr = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
517-
net_addr = CNetAddr{v4_addr};
518-
} else if (network == Network::NET_IPV6) {
519-
if (fuzzed_data_provider.remaining_bytes() >= 16) {
520-
in6_addr v6_addr = {};
521-
memcpy(v6_addr.s6_addr, fuzzed_data_provider.ConsumeBytes<uint8_t>(16).data(), 16);
522-
net_addr = CNetAddr{v6_addr, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
523-
}
524-
} else if (network == Network::NET_INTERNAL) {
525-
net_addr.SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
526-
} else if (network == Network::NET_ONION) {
527-
net_addr.SetSpecial(fuzzed_data_provider.ConsumeBytesAsString(32));
528-
}
529-
return net_addr;
530-
}
531-
532511
CAddress ConsumeAddress(FuzzedDataProvider& fuzzed_data_provider) noexcept
533512
{
534513
return {ConsumeService(fuzzed_data_provider), ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS), NodeSeconds{std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<uint32_t>()}}};

src/test/fuzz/util.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <streams.h>
2323
#include <test/fuzz/FuzzedDataProvider.h>
2424
#include <test/fuzz/fuzz.h>
25+
#include <test/fuzz/util/net.h>
2526
#include <test/util/net.h>
2627
#include <uint256.h>
2728
#include <version.h>
@@ -283,8 +284,6 @@ inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
283284
return result;
284285
}
285286

286-
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept;
287-
288287
inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
289288
{
290289
return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};

0 commit comments

Comments
 (0)