Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#22144: Randomize message processing peer order
Browse files Browse the repository at this point in the history
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 #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/bitcoin@79c02c8
  jonatack:
    ACK 79c02c8
  vasild:
    ACK 79c02c8
  theStack:
    ACK 79c02c8

Tree-SHA512: 9a87c4dcad47c2d61b76c4f37f59674876b78f33f45943089bf159902a23e12de7a5feae1a73b17cbc3f2e37c980ecf0f7fd86af9e6fa3a68099537a3c82c106
  • Loading branch information
fanquake committed Jun 16, 2021
2 parents eb63b1d + 79c02c8 commit 6bc1eca
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai

void CConnman::ThreadMessageHandler()
{
FastRandomContext rng;
while (!flagInterruptMsgProc)
{
std::vector<CNode*> vNodesCopy;
Expand All @@ -2226,6 +2227,11 @@ void CConnman::ThreadMessageHandler()

bool fMoreWork = false;

// Randomize the order in which we process messages from/to our peers.
// This prevents attacks in which an attacker exploits having multiple
// consecutive connections in the vNodes list.
Shuffle(vNodesCopy.begin(), vNodesCopy.end(), rng);

for (CNode* pnode : vNodesCopy)
{
if (pnode->fDisconnect)
Expand Down

0 comments on commit 6bc1eca

Please sign in to comment.