Skip to content

Commit df73438

Browse files
authored
Fix node protection logic false positives (#3314)
We could be reading multiple messages from a socket buffer at once _without actually processing them yet_ which means that `fSuccessfullyConnected` might not be switched to `true` at the time we already parsed `VERACK` message and started to parse the next one. This is basically a false positive and we drop a legit node as a result even though the order of messages sent by this node was completely fine. To fix this I partially reverted #2790 (where the issue was initially introduced) and moved the logic for tracking the first message into ProcessMessage instead.
1 parent 0fee42e commit df73438

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

src/net.cpp

+1-19
Original file line numberDiff line numberDiff line change
@@ -776,24 +776,6 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
776776
int handled;
777777
if (!msg.in_data) {
778778
handled = msg.readHeader(pch, nBytes);
779-
if (msg.in_data && nTimeFirstMessageReceived == 0) {
780-
if (fSuccessfullyConnected) {
781-
// First message after VERSION/VERACK.
782-
// We record the time when the header is fully received and not when the full message is received.
783-
// otherwise a peer might send us a very large message as first message after VERSION/VERACK and fill
784-
// up our memory with multiple parallel connections doing this.
785-
nTimeFirstMessageReceived = nTimeMicros;
786-
fFirstMessageIsMNAUTH = msg.hdr.GetCommand() == NetMsgType::MNAUTH;
787-
} else {
788-
// We're still in the VERSION/VERACK handshake process, so any message received in this state is
789-
// expected to be very small. This protects against attackers filling up memory by sending oversized
790-
// VERSION messages while the incoming connection is still protected against eviction
791-
if (msg.hdr.nMessageSize > 1024) {
792-
LogPrint(BCLog::NET, "Oversized VERSION/VERACK message from peer=%i, disconnecting\n", GetId());
793-
return false;
794-
}
795-
}
796-
}
797779
} else {
798780
handled = msg.readData(pch, nBytes);
799781
}
@@ -1050,7 +1032,7 @@ bool CConnman::AttemptToEvictConnection()
10501032
if (fMasternodeMode) {
10511033
// This handles eviction protected nodes. Nodes are always protected for a short time after the connection
10521034
// was accepted. This short time is meant for the VERSION/VERACK exchange and the possible MNAUTH that might
1053-
// follow when the incoming connection is from another masternode. When another message then MNAUTH
1035+
// follow when the incoming connection is from another masternode. When a message other than MNAUTH
10541036
// is received after VERSION/VERACK, the protection is lifted immediately.
10551037
bool isProtected = GetSystemTimeInSeconds() - node->nTimeConnected < INBOUND_EVICTION_PROTECTION_TIME;
10561038
if (node->nTimeFirstMessageReceived != 0 && !node->fFirstMessageIsMNAUTH) {

src/net_processing.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,13 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20412041
return false;
20422042
}
20432043

2044+
if (pfrom->nTimeFirstMessageReceived == 0) {
2045+
// First message after VERSION/VERACK
2046+
pfrom->nTimeFirstMessageReceived = GetTimeMicros();
2047+
pfrom->fFirstMessageIsMNAUTH = strCommand == NetMsgType::MNAUTH;
2048+
// Note: do not break the flow here
2049+
}
2050+
20442051
if (strCommand == NetMsgType::ADDR) {
20452052
std::vector<CAddress> vAddr;
20462053
vRecv >> vAddr;

0 commit comments

Comments
 (0)