Skip to content

Commit 12afb98

Browse files
laanwjkwvg
authored andcommitted
Merge bitcoin#20140: Restore compatibility with old CSubNet serialization
886be97 Ignore incorrectly-serialized banlist.dat entries (Pieter Wuille) 883cea7 Restore compatibility with old CSubNet serialization (Pieter Wuille) Pull request description: bitcoin#19628 changed CSubNet for IPv4 netmasks, using the first 4 bytes of `netmask` rather than the last 4 to store the actual mask. Unfortunately, CSubNet objects are serialized on disk in banlist.dat, breaking compatibility with existing banlists (and bringing them into an inconsistent state where entries reported in `listbanned` cannot be removed). Fix this by reverting to the old format (just for serialization). Also add a sanity check to the deserializer so that nonsensical banlist.dat entries are ignored (which would otherwise be possible if someone added IPv4 entries after bitcoin#19628 but without this PR). Reported by Greg Maxwell. ACKs for top commit: laanwj: Code review ACK 886be97 vasild: ACK 886be97 Tree-SHA512: d3fb91e8ecd933406e527187974f22770374ee2e12a233e7870363f52ecda471fb0b7bae72420e8ff6b6b1594e3037a5115984c023dbadf38f86aeaffcd681e7
1 parent 41092bc commit 12afb98

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/banman.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void BanMan::SweepBanned()
192192
while (it != m_banned.end()) {
193193
CSubNet sub_net = (*it).first;
194194
CBanEntry ban_entry = (*it).second;
195-
if (now > ban_entry.nBanUntil) {
195+
if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
196196
m_banned.erase(it++);
197197
m_is_dirty = true;
198198
notify_ui = true;

src/netaddress.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,17 @@ bool CSubNet::IsValid() const
10761076
return valid;
10771077
}
10781078

1079+
bool CSubNet::SanityCheck() const
1080+
{
1081+
if (!(network.IsIPv4() || network.IsIPv6())) return false;
1082+
1083+
for (size_t x = 0; x < network.m_addr.size(); ++x) {
1084+
if (network.m_addr[x] & ~netmask[x]) return false;
1085+
}
1086+
1087+
return true;
1088+
}
1089+
10791090
bool operator==(const CSubNet& a, const CSubNet& b)
10801091
{
10811092
return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);

src/netaddress.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ class CSubNet
461461
/// Is this value valid? (only used to signal parse errors)
462462
bool valid;
463463

464+
bool SanityCheck() const;
465+
464466
public:
465467
CSubNet();
466468
CSubNet(const CNetAddr& addr, uint8_t mask);
@@ -478,7 +480,23 @@ class CSubNet
478480
friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
479481
friend bool operator<(const CSubNet& a, const CSubNet& b);
480482

481-
SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
483+
SERIALIZE_METHODS(CSubNet, obj)
484+
{
485+
READWRITE(obj.network);
486+
if (obj.network.IsIPv4()) {
487+
// Before commit 102867c587f5f7954232fb8ed8e85cda78bb4d32, CSubNet used the last 4 bytes of netmask
488+
// to store the relevant bytes for an IPv4 mask. For compatiblity reasons, keep doing so in
489+
// serialized form.
490+
unsigned char dummy[12] = {0};
491+
READWRITE(dummy);
492+
READWRITE(MakeSpan(obj.netmask).first(4));
493+
} else {
494+
READWRITE(obj.netmask);
495+
}
496+
READWRITE(obj.valid);
497+
// Mark invalid if the result doesn't pass sanity checking.
498+
SER_READ(obj, if (obj.valid) obj.valid = obj.SanityCheck());
499+
}
482500
};
483501

484502
/** A combination of a network address (CNetAddr) and a (TCP) port */

0 commit comments

Comments
 (0)