Skip to content

Commit 366ca98

Browse files
fanquakePastaPastaPasta
authored andcommitted
Merge bitcoin#22144: Randomize message processing peer order
79c02c8 Randomize message processing peer order (Pieter Wuille) Pull request description: Right now, the message handling loop iterates the list of nodes always in the same order: the order they were connected in (see the `vNodes` vector). For some parts of the net processing logic, this order matters. Transaction requests are assigned explicitly to peers since bitcoin#19988, but many other parts of processing work on a "first-served-by-loop-first" basis, such as block downloading. If peers can predict this ordering, it may be exploited to cause delays. As there isn't anything particularly optimal about the current ordering, just make it unpredictable by randomizing. Reported by Crypt-iQ. ACKs for top commit: jnewbery: ACK 79c02c8 Crypt-iQ: ACK 79c02c8 sdaftuar: utACK 79c02c8 achow101: Code Review ACK 79c02c8 jamesob: crACK bitcoin@79c02c8 jonatack: ACK 79c02c8 vasild: ACK 79c02c8 theStack: ACK 79c02c8 Tree-SHA512: 9a87c4dcad47c2d61b76c4f37f59674876b78f33f45943089bf159902a23e12de7a5feae1a73b17cbc3f2e37c980ecf0f7fd86af9e6fa3a68099537a3c82c106
1 parent 4d20cb7 commit 366ca98

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/net.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,7 @@ void CConnman::ThreadMessageHandler()
29652965
{
29662966
int64_t nLastSendMessagesTimeMasternodes = 0;
29672967

2968+
FastRandomContext rng;
29682969
while (!flagInterruptMsgProc)
29692970
{
29702971
std::vector<CNode*> vNodesCopy = CopyNodeVector();
@@ -2976,6 +2977,10 @@ void CConnman::ThreadMessageHandler()
29762977
fSkipSendMessagesForMasternodes = false;
29772978
nLastSendMessagesTimeMasternodes = GetTimeMillis();
29782979
}
2980+
// Randomize the order in which we process messages from/to our peers.
2981+
// This prevents attacks in which an attacker exploits having multiple
2982+
// consecutive connections in the vNodes list.
2983+
Shuffle(vNodesCopy.begin(), vNodesCopy.end(), rng);
29792984

29802985
for (CNode* pnode : vNodesCopy)
29812986
{

0 commit comments

Comments
 (0)