Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
banman.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-2022 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 <banman.h>
6#include <common/args.h>
7#include <netaddress.h>
9#include <test/fuzz/fuzz.h>
10#include <test/fuzz/util.h>
11#include <test/fuzz/util/net.h>
13#include <util/fs.h>
14#include <util/readwritefile.h>
15
16#include <cassert>
17#include <cstdint>
18#include <limits>
19#include <string>
20#include <vector>
21
22namespace {
23int64_t ConsumeBanTimeOffset(FuzzedDataProvider& fuzzed_data_provider) noexcept
24{
25 // Avoid signed integer overflow by capping to int32_t max:
26 // banman.cpp:137:73: runtime error: signed integer overflow: 1591700817 + 9223372036854775807 cannot be represented in type 'long'
27 return fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(std::numeric_limits<int64_t>::min(), std::numeric_limits<int32_t>::max());
28}
29} // namespace
30
32{
33 static const auto testing_setup = MakeNoLogFileContext<>();
34}
35
36static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
37{
38 return lhs.nVersion == rhs.nVersion &&
39 lhs.nCreateTime == rhs.nCreateTime &&
40 lhs.nBanUntil == rhs.nBanUntil;
41}
42
44{
45 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
46 SetMockTime(ConsumeTime(fuzzed_data_provider));
47 fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist";
48
49 const bool start_with_corrupted_banlist{fuzzed_data_provider.ConsumeBool()};
50 bool force_read_and_write_to_err{false};
51 if (start_with_corrupted_banlist) {
52 assert(WriteBinaryFile(banlist_file + ".json",
53 fuzzed_data_provider.ConsumeRandomLengthString()));
54 } else {
55 force_read_and_write_to_err = fuzzed_data_provider.ConsumeBool();
56 if (force_read_and_write_to_err) {
57 banlist_file = fs::path{"path"} / "to" / "inaccessible" / "fuzzed_banlist";
58 }
59 }
60
61 {
62 BanMan ban_man{banlist_file, /*client_interface=*/nullptr, /*default_ban_time=*/ConsumeBanTimeOffset(fuzzed_data_provider)};
63 // The complexity is O(N^2), where N is the input size, because each call
64 // might call DumpBanlist (or other methods that are at least linear
65 // complexity of the input size).
66 bool contains_invalid{false};
67 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
68 {
70 fuzzed_data_provider,
71 [&] {
72 CNetAddr net_addr{ConsumeNetAddr(fuzzed_data_provider)};
73 if (!net_addr.IsCJDNS() || !net_addr.IsValid()) {
74 const std::optional<CNetAddr>& addr{LookupHost(net_addr.ToStringAddr(), /*fAllowLookup=*/false)};
75 if (addr.has_value() && addr->IsValid()) {
76 net_addr = *addr;
77 } else {
78 contains_invalid = true;
79 }
80 }
81 ban_man.Ban(net_addr, ConsumeBanTimeOffset(fuzzed_data_provider), fuzzed_data_provider.ConsumeBool());
82 },
83 [&] {
84 CSubNet subnet{ConsumeSubNet(fuzzed_data_provider)};
85 subnet = LookupSubNet(subnet.ToString());
86 if (!subnet.IsValid()) {
87 contains_invalid = true;
88 }
89 ban_man.Ban(subnet, ConsumeBanTimeOffset(fuzzed_data_provider), fuzzed_data_provider.ConsumeBool());
90 },
91 [&] {
92 ban_man.ClearBanned();
93 },
94 [&] {
95 ban_man.IsBanned(ConsumeNetAddr(fuzzed_data_provider));
96 },
97 [&] {
98 ban_man.IsBanned(ConsumeSubNet(fuzzed_data_provider));
99 },
100 [&] {
101 ban_man.Unban(ConsumeNetAddr(fuzzed_data_provider));
102 },
103 [&] {
104 ban_man.Unban(ConsumeSubNet(fuzzed_data_provider));
105 },
106 [&] {
107 banmap_t banmap;
108 ban_man.GetBanned(banmap);
109 },
110 [&] {
111 ban_man.DumpBanlist();
112 },
113 [&] {
114 ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider));
115 });
116 }
117 if (!force_read_and_write_to_err) {
118 ban_man.DumpBanlist();
119 SetMockTime(ConsumeTime(fuzzed_data_provider));
120 banmap_t banmap;
121 ban_man.GetBanned(banmap);
122 BanMan ban_man_read{banlist_file, /*client_interface=*/nullptr, /*default_ban_time=*/0};
123 banmap_t banmap_read;
124 ban_man_read.GetBanned(banmap_read);
125 if (!contains_invalid) {
126 assert(banmap == banmap_read);
127 }
128 }
129 }
130 fs::remove(fs::PathToString(banlist_file + ".json"));
131}
ArgsManager gArgs
Definition args.cpp:41
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition args.h:232
void GetBanned(banmap_t &banmap) EXCLUSIVE_LOCKS_REQUIRED(!m_banned_mutex)
Definition banman.cpp:174
Definition net_types.h:15
int64_t nCreateTime
Definition net_types.h:19
int nVersion
Definition net_types.h:18
int64_t nBanUntil
Definition net_types.h:20
Network address.
Definition netaddress.h:112
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:33
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition fs.h:151
std::map< CSubNet, CBanEntry > banmap_t
Definition net_types.h:41
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition netbase.cpp:755
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition netbase.cpp:177
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
Write contents of std::string to a file.
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
void initialize_banman()
Definition banman.cpp:31
static bool operator==(const CBanEntry &lhs, const CBanEntry &rhs)
Definition banman.cpp:36
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition net.cpp:28
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition net.h:96
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition util.cpp:34
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:35
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:32
assert(!tx.IsCoinBase())