2021-01-25 12:23:45 +01:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
2020-06-09 11:16:13 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <banman.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <netaddress.h>
|
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
|
|
#include <test/fuzz/fuzz.h>
|
|
|
|
#include <test/fuzz/util.h>
|
2021-01-25 12:22:03 +01:00
|
|
|
#include <test/util/setup_common.h>
|
2021-01-14 09:33:04 +01:00
|
|
|
#include <util/readwritefile.h>
|
2020-06-09 11:16:13 +00:00
|
|
|
#include <util/system.h>
|
|
|
|
|
2021-01-14 09:33:04 +01:00
|
|
|
#include <cassert>
|
2020-06-09 11:16:13 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int64_t ConsumeBanTimeOffset(FuzzedDataProvider& fuzzed_data_provider) noexcept
|
|
|
|
{
|
|
|
|
// Avoid signed integer overflow by capping to int32_t max:
|
|
|
|
// banman.cpp:137:73: runtime error: signed integer overflow: 1591700817 + 9223372036854775807 cannot be represented in type 'long'
|
|
|
|
return fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(std::numeric_limits<int64_t>::min(), std::numeric_limits<int32_t>::max());
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
void initialize_banman()
|
2020-06-09 11:16:13 +00:00
|
|
|
{
|
2021-01-25 12:23:45 +01:00
|
|
|
static const auto testing_setup = MakeNoLogFileContext<>();
|
2020-06-09 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:28:30 +02:00
|
|
|
static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
|
|
|
|
{
|
|
|
|
return lhs.nVersion == rhs.nVersion &&
|
|
|
|
lhs.nCreateTime == rhs.nCreateTime &&
|
|
|
|
lhs.nBanUntil == rhs.nBanUntil;
|
|
|
|
}
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
FUZZ_TARGET_INIT(banman, initialize_banman)
|
2020-06-09 11:16:13 +00:00
|
|
|
{
|
|
|
|
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
2020-11-19 21:25:14 +00:00
|
|
|
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
2021-01-14 09:33:04 +01:00
|
|
|
fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist";
|
|
|
|
|
|
|
|
const bool start_with_corrupted_banlist{fuzzed_data_provider.ConsumeBool()};
|
2021-06-23 13:28:30 +02:00
|
|
|
bool force_read_and_write_to_err{false};
|
2021-01-14 09:33:04 +01:00
|
|
|
if (start_with_corrupted_banlist) {
|
2021-07-28 20:00:23 +02:00
|
|
|
assert(WriteBinaryFile(banlist_file.string() + ".json",
|
2021-01-14 09:33:04 +01:00
|
|
|
fuzzed_data_provider.ConsumeRandomLengthString()));
|
|
|
|
} else {
|
2021-06-23 13:28:30 +02:00
|
|
|
force_read_and_write_to_err = fuzzed_data_provider.ConsumeBool();
|
2021-01-14 09:33:04 +01:00
|
|
|
if (force_read_and_write_to_err) {
|
|
|
|
banlist_file = fs::path{"path"} / "to" / "inaccessible" / "fuzzed_banlist";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 11:16:13 +00:00
|
|
|
{
|
2021-06-23 13:28:30 +02:00
|
|
|
BanMan ban_man{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ ConsumeBanTimeOffset(fuzzed_data_provider)};
|
2021-08-21 19:34:13 +02:00
|
|
|
// The complexity is O(N^2), where N is the input size, because each call
|
|
|
|
// might call DumpBanlist (or other methods that are at least linear
|
|
|
|
// complexity of the input size).
|
|
|
|
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
|
|
|
|
{
|
2021-01-02 13:38:14 +01:00
|
|
|
CallOneOf(
|
|
|
|
fuzzed_data_provider,
|
|
|
|
[&] {
|
|
|
|
ban_man.Ban(ConsumeNetAddr(fuzzed_data_provider),
|
|
|
|
ConsumeBanTimeOffset(fuzzed_data_provider), fuzzed_data_provider.ConsumeBool());
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.Ban(ConsumeSubNet(fuzzed_data_provider),
|
|
|
|
ConsumeBanTimeOffset(fuzzed_data_provider), fuzzed_data_provider.ConsumeBool());
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.ClearBanned();
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.IsBanned(ConsumeNetAddr(fuzzed_data_provider));
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.IsBanned(ConsumeSubNet(fuzzed_data_provider));
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.Unban(ConsumeNetAddr(fuzzed_data_provider));
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.Unban(ConsumeSubNet(fuzzed_data_provider));
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
banmap_t banmap;
|
|
|
|
ban_man.GetBanned(banmap);
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.DumpBanlist();
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider));
|
|
|
|
});
|
2020-06-09 11:16:13 +00:00
|
|
|
}
|
2021-06-23 13:28:30 +02:00
|
|
|
if (!force_read_and_write_to_err) {
|
|
|
|
ban_man.DumpBanlist();
|
|
|
|
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
|
|
|
banmap_t banmap;
|
|
|
|
ban_man.GetBanned(banmap);
|
|
|
|
BanMan ban_man_read{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ 0};
|
|
|
|
banmap_t banmap_read;
|
|
|
|
ban_man_read.GetBanned(banmap_read);
|
2021-08-07 10:27:47 +02:00
|
|
|
assert(banmap == banmap_read);
|
2021-06-23 13:28:30 +02:00
|
|
|
}
|
2020-06-09 11:16:13 +00:00
|
|
|
}
|
2021-01-14 09:33:04 +01:00
|
|
|
fs::remove(banlist_file.string() + ".json");
|
2020-06-09 11:16:13 +00:00
|
|
|
}
|