Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize message processing peer order #22144

Merged
merged 1 commit into from
Jun 16, 2021

Conversation

sipa
Copy link
Member

@sipa sipa commented Jun 3, 2021

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.

@fanquake fanquake added the P2P label Jun 3, 2021
@practicalswift
Copy link
Contributor

Concept ACK

1 similar comment
@theStack
Copy link
Contributor

theStack commented Jun 3, 2021

Concept ACK

@ghost
Copy link

ghost commented Jun 3, 2021

Concept ACK

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.

I am assuming it improves privacy and security.

@Crypt-iQ
Copy link
Contributor

Crypt-iQ commented Jun 3, 2021

Concept ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 4, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@jnewbery
Copy link
Contributor

jnewbery commented Jun 4, 2021

ACK 79c02c8

We shouldn't build any assumptions into net_processing about the order that we serve peers. Shuffling the order on each loop of the message handler is a good way to make that requirement more explicit.

I think in the future, we want to move away from a strictly equally-weighted algorithm to service our peers in the message handling thread. If a peer is providing us with lots of useful data, we shouldn't have to call ProcessMessages()/SendMessages() for every peer each time we process a message from the useful peer. Likewise, if a peer is causing us to use a lot of resources, we should be able to throttle back on serving it. That would mean a non-stable ordering of servicing peers, which would again mean that net_processing can't assume a stable ordering. Merging this PR should give us confidence that there aren't any undocumented assumptions about ordering in net_processing and that those future changes are safe.

In summary, I think this is a useful change now, and is consistent with the way we should think about the net-net_processing interface.

@kristapsk
Copy link
Contributor

Concept ACK

@Crypt-iQ
Copy link
Contributor

Crypt-iQ commented Jun 4, 2021

ACK 79c02c8

@sipa
Copy link
Member Author

sipa commented Jun 4, 2021

FWIW, shuffling 1000 pointers takes around 10 microseconds here, if anyone was concerned about the CPU cost (in a microbenchmark).

Copy link
Contributor

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, shuffling 1000 pointers takes around 10 microseconds here, if anyone was concerned about the CPU cost (in a microbenchmark).

Thanks, I was curious what the impact would be.

I've been running a node with this patch rebased to master (to also continue testing the torv2 removal) since a few minutes after this was opened, as a sanity check, and all seems nominal.

ACK 79c02c8

@sdaftuar
Copy link
Member

sdaftuar commented Jun 7, 2021

utACK 79c02c8

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 79c02c8

The patch looks harmless and will achieve what it claims. Maybe it is just me, but I don't see the attack vector.

@luke-jr
Copy link
Member

luke-jr commented Jun 12, 2021

Is there a risk this could make things worse? Right now, longer-connected peers can't be "disrupted" (in this context) by later-connected peers, but with a random order, disruption opportunities also open up to later peers where they might not have previously been.

@sipa
Copy link
Member Author

sipa commented Jun 12, 2021

@luke-jr Interruption isn't changed, we always progress from one peer to the next one in vNodes to the next one in the list, at predefined times (when all its messages have been processed, or after certain things like getdata for blocks). What this changes is removing the predictability of that order.

@jamesob
Copy link
Member

jamesob commented Jun 12, 2021

Concept ACK

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 79c02c8

Built and ran the PR branch on mainnet for the last few hours, didn't notice any problems.

@sipa sipa added this to the 22.0 milestone Jun 14, 2021
@achow101
Copy link
Member

Code Review ACK 79c02c8

@jamesob
Copy link
Member

jamesob commented Jun 15, 2021

crACK 79c02c8

@fanquake
Copy link
Member

Edited op to remove @ mention.

@fanquake fanquake merged commit 6bc1eca into bitcoin:master Jun 16, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 16, 2021
@vasild
Copy link
Contributor

vasild commented Jun 21, 2021

Should this shuffling also be done in CConnman::SocketHandler()? I will do that in #21943 mainly because it is most clean to create the nodes' snapshot shuffled.

gwillen pushed a commit to ElementsProject/elements that referenced this pull request Jun 1, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet