Skip to content

p2p: rate-limit incoming connections by IP#6286

Merged
tychoish merged 10 commits intotendermint:masterfrom
tychoish:p2p-rate-limit-connections
Mar 29, 2021
Merged

p2p: rate-limit incoming connections by IP#6286
tychoish merged 10 commits intotendermint:masterfrom
tychoish:p2p-rate-limit-connections

Conversation

@tychoish
Copy link
Contributor

implements simple incoming peer connection rate limiting both by time
window to prevent connection storms, and by count to prevent multiple
connections from the same peer.

This happens at the router level which means the transport layer could
still present a vector for some kind of disruption.

@tychoish tychoish marked this pull request as draft March 26, 2021 20:55
@codecov
Copy link

codecov bot commented Mar 29, 2021

Codecov Report

Merging #6286 (d237bcb) into master (91506bf) will decrease coverage by 0.05%.
The diff coverage is 76.47%.

@@            Coverage Diff             @@
##           master    #6286      +/-   ##
==========================================
- Coverage   60.90%   60.84%   -0.06%     
==========================================
  Files         281      282       +1     
  Lines       26660    26707      +47     
==========================================
+ Hits        16237    16251      +14     
- Misses       8738     8771      +33     
  Partials     1685     1685              
Impacted Files Coverage Δ
p2p/router.go 78.03% <58.33%> (-1.50%) ⬇️
p2p/conn_tracker.go 92.59% <92.59%> (ø)
libs/events/events.go 92.94% <0.00%> (-5.89%) ⬇️
blockchain/v0/pool.go 74.52% <0.00%> (-5.33%) ⬇️
privval/signer_server.go 89.47% <0.00%> (-5.27%) ⬇️
privval/signer_endpoint.go 78.78% <0.00%> (-3.04%) ⬇️
p2p/pex/pex_reactor.go 76.55% <0.00%> (-2.08%) ⬇️
evidence/reactor.go 72.80% <0.00%> (-1.76%) ⬇️
blockchain/v0/reactor.go 63.20% <0.00%> (-1.60%) ⬇️
p2p/transport_memory.go 85.13% <0.00%> (-1.36%) ⬇️
... and 12 more

@tychoish tychoish marked this pull request as ready for review March 29, 2021 16:37
Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Great work @tychoish 👍


switch {
case o.IncomingConnectionWindow == 0:
o.IncomingConnectionWindow = 100 * time.Millisecond
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 100ms kind of arbitrary or do you have some gut judgement here? (totally cool with either answer :-p)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

some of both? Mostly arbitrary, but also I can't imagine connection attempts more frequent than 100ms being a viable use case?

RemoveConn(net.IP)
}

type connTrackerImpl struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this implementation should do the trick. Any reason you opted for a in-house implementation instead of a ready-to-use rate limier, e.g. https://github.com/sethvargo/go-limiter?

The above already garbage collects and maps directly to our API & needs. We can certainly keep this implementation, but I was just curious. Generally in favor of having to maintain less code :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sense. I think it's pretty simple, and I don't see places where we're using go-limiter otherwise (and it looks like we'd need some kind of wrapper code/etc. anyway, so I'm not sure if it's a huge burden.) I put the rate-limiting behind an interface so we could change it later if we needed.

@alexanderbez alexanderbez added the C:p2p Component: P2P pkg label Mar 29, 2021
@alexanderbez alexanderbez requested a review from cmwaters March 29, 2021 20:59
@tychoish tychoish merged commit c62e320 into tendermint:master Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C:p2p Component: P2P pkg

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants