-
Notifications
You must be signed in to change notification settings - Fork 31
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
Discv5 Protocol: Add support for banning nodes #769
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c0c3ba0
Add banned nodes to routing table.
bhartnett 59b17d1
Add tests.
bhartnett 6cdd9cd
Fix test.
bhartnett e6a542c
Reduce sleep time in test.
bhartnett 52b72df
Cleanup expired bans from banned nodes table when adding a new ban.
bhartnett f672d72
Make cleanupExpiredBans public so it can be called independently of b…
bhartnett 1e8110c
Init bannedNodes field.
bhartnett e16360b
Filter out banned nodes in lookups and cleanup expired bans in refres…
bhartnett 10b1e4d
Don't respond to messages from banned nodes.
bhartnett 218d626
Minor fix.
bhartnett dd9962d
Merge branch 'master' into discv5-ban-list
bhartnett 854f17c
Prevent sending messages to banned nodes.
bhartnett aab3ca0
Add Discv5 banned nodes enabled tests.
bhartnett c1d76ce
Don't ban nodes on timeout.
bhartnett d522c71
Merge branch 'master' into discv5-ban-list
bhartnett 9f5c274
Add specific tests covering banned nodes.
bhartnett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,9 @@ const | |
defaultResponseTimeout* = 4.seconds ## timeout for the response of a request-response | ||
## call | ||
|
||
## Ban durations for banned nodes in the routing table | ||
NodeBanDurationInvalidResponse = 15.minutes | ||
|
||
type | ||
OptAddress* = object | ||
ip*: Opt[IpAddress] | ||
|
@@ -142,6 +145,7 @@ type | |
bindAddress: OptAddress ## UDP binding address | ||
pendingRequests: Table[AESGCMNonce, PendingRequest] | ||
routingTable*: RoutingTable | ||
banNodes: bool | ||
codec*: Codec | ||
awaitedMessages: Table[(NodeId, RequestId), Future[Opt[Message]]] | ||
refreshLoop: Future[void] | ||
|
@@ -157,6 +161,7 @@ type | |
responseTimeout: Duration | ||
rng*: ref HmacDrbgContext | ||
|
||
|
||
PendingRequest = object | ||
node: Node | ||
message: seq[byte] | ||
|
@@ -192,10 +197,13 @@ proc addNode*(d: Protocol, node: Node): bool = | |
## | ||
## Returns true only when `Node` was added as a new entry to a bucket in the | ||
## routing table. | ||
if d.routingTable.addNode(node) == Added: | ||
let r = d.routingTable.addNode(node) | ||
if r == Added: | ||
return true | ||
else: | ||
return false | ||
|
||
if r == Banned: | ||
debug "Banned node not added to routing table", nodeId = node.id | ||
return false | ||
|
||
proc addNode*(d: Protocol, r: Record): bool = | ||
## Add `Node` from a `Record` to discovery routing table. | ||
|
@@ -429,6 +437,30 @@ proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address, | |
else: | ||
debug "Node with this id already has ongoing handshake, ignoring packet" | ||
|
||
proc replaceNode(d: Protocol, n: Node) = | ||
if n.record notin d.bootstrapRecords: | ||
d.routingTable.replaceNode(n) | ||
else: | ||
# For now we never remove bootstrap nodes. It might make sense to actually | ||
# do so and to retry them only in case we drop to a really low amount of | ||
# peers in the routing table. | ||
debug "Message request to bootstrap node failed", enr = toURI(n.record) | ||
|
||
proc banNode*(d: Protocol, n: Node, banPeriod: chronos.Duration) = | ||
if n.record notin d.bootstrapRecords: | ||
if d.banNodes: | ||
d.routingTable.banNode(n.id, banPeriod) # banNode also replaces the node | ||
else: | ||
d.routingTable.replaceNode(n) | ||
else: | ||
# For now we never remove bootstrap nodes. It might make sense to actually | ||
# do so and to retry them only in case we drop to a really low amount of | ||
# peers in the routing table. | ||
debug "Message request to bootstrap node failed", enr = toURI(n.record) | ||
|
||
proc isBanned*(d: Protocol, nodeId: NodeId): bool = | ||
d.banNodes and d.routingTable.isBanned(nodeId) | ||
|
||
proc receive*(d: Protocol, a: Address, packet: openArray[byte]) = | ||
discv5_network_bytes.inc(packet.len.int64, labelValues = [$Direction.In]) | ||
|
||
|
@@ -437,6 +469,10 @@ proc receive*(d: Protocol, a: Address, packet: openArray[byte]) = | |
let packet = decoded[] | ||
case packet.flag | ||
of OrdinaryMessage: | ||
if d.isBanned(packet.srcId): | ||
trace "Ignoring received OrdinaryMessage from banned node", nodeId = packet.srcId | ||
return | ||
|
||
if packet.messageOpt.isSome(): | ||
let message = packet.messageOpt.get() | ||
trace "Received message packet", srcId = packet.srcId, address = a, | ||
|
@@ -464,6 +500,10 @@ proc receive*(d: Protocol, a: Address, packet: openArray[byte]) = | |
else: | ||
debug "Timed out or unrequested whoareyou packet", address = a | ||
of HandshakeMessage: | ||
if d.isBanned(packet.srcIdHs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem above. |
||
trace "Ignoring received HandshakeMessage from banned node", nodeId = packet.srcIdHs | ||
return | ||
|
||
trace "Received handshake message packet", srcId = packet.srcIdHs, | ||
address = a, kind = packet.message.kind | ||
d.handleMessage(packet.srcIdHs, a, packet.message, packet.node) | ||
|
@@ -494,14 +534,7 @@ proc processClient(transp: DatagramTransport, raddr: TransportAddress): | |
|
||
proto.receive(Address(ip: raddr.toIpAddress(), port: raddr.port), buf) | ||
|
||
proc replaceNode(d: Protocol, n: Node) = | ||
if n.record notin d.bootstrapRecords: | ||
d.routingTable.replaceNode(n) | ||
else: | ||
# For now we never remove bootstrap nodes. It might make sense to actually | ||
# do so and to retry them only in case we drop to a really low amount of | ||
# peers in the routing table. | ||
debug "Message request to bootstrap node failed", enr = toURI(n.record) | ||
|
||
|
||
# TODO: This could be improved to do the clean-up immediately in case a non | ||
# whoareyou response does arrive, but we would need to store the AuthTag | ||
|
@@ -546,9 +579,11 @@ proc waitNodes(d: Protocol, fromNode: Node, reqId: RequestId): | |
break | ||
return ok(res) | ||
else: | ||
d.banNode(fromNode, NodeBanDurationInvalidResponse) | ||
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"]) | ||
return err("Invalid response to find node message") | ||
else: | ||
d.replaceNode(fromNode) | ||
discovery_message_requests_outgoing.inc(labelValues = ["no_response"]) | ||
return err("Nodes message not received in time") | ||
|
||
|
@@ -574,6 +609,10 @@ proc ping*(d: Protocol, toNode: Node): | |
## Send a discovery ping message. | ||
## | ||
## Returns the received pong message or an error. | ||
|
||
if d.isBanned(toNode.id): | ||
return err("toNode is banned") | ||
|
||
let reqId = d.sendMessage(toNode, | ||
PingMessage(enrSeq: d.localNode.record.seqNum)) | ||
let resp = await d.waitMessage(toNode, reqId) | ||
|
@@ -583,7 +622,7 @@ proc ping*(d: Protocol, toNode: Node): | |
d.routingTable.setJustSeen(toNode) | ||
return ok(resp.get().pong) | ||
else: | ||
d.replaceNode(toNode) | ||
d.banNode(toNode, NodeBanDurationInvalidResponse) | ||
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"]) | ||
return err("Invalid response to ping message") | ||
else: | ||
|
@@ -597,22 +636,29 @@ proc findNode*(d: Protocol, toNode: Node, distances: seq[uint16]): | |
## | ||
## Returns the received nodes or an error. | ||
## Received ENRs are already validated and converted to `Node`. | ||
|
||
if d.isBanned(toNode.id): | ||
return err("toNode is banned") | ||
|
||
let reqId = d.sendMessage(toNode, FindNodeMessage(distances: distances)) | ||
let nodes = await d.waitNodes(toNode, reqId) | ||
|
||
if nodes.isOk: | ||
let res = verifyNodesRecords(nodes.get(), toNode, findNodeResultLimit, distances) | ||
d.routingTable.setJustSeen(toNode) | ||
return ok(res) | ||
return ok(res.filterIt(not d.isBanned(it.id))) | ||
else: | ||
d.replaceNode(toNode) | ||
return err(nodes.error) | ||
|
||
proc talkReq*(d: Protocol, toNode: Node, protocol, request: seq[byte]): | ||
Future[DiscResult[seq[byte]]] {.async: (raises: [CancelledError]).} = | ||
## Send a discovery talkreq message. | ||
## | ||
## Returns the received talkresp message or an error. | ||
|
||
if d.isBanned(toNode.id): | ||
return err("toNode is banned") | ||
|
||
let reqId = d.sendMessage(toNode, | ||
TalkReqMessage(protocol: protocol, request: request)) | ||
let resp = await d.waitMessage(toNode, reqId) | ||
|
@@ -622,7 +668,7 @@ proc talkReq*(d: Protocol, toNode: Node, protocol, request: seq[byte]): | |
d.routingTable.setJustSeen(toNode) | ||
return ok(resp.get().talkResp.response) | ||
else: | ||
d.replaceNode(toNode) | ||
d.banNode(toNode, NodeBanDurationInvalidResponse) | ||
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"]) | ||
return err("Invalid response to talk request message") | ||
else: | ||
|
@@ -797,6 +843,12 @@ proc resolve*(d: Protocol, id: NodeId): Future[Opt[Node]] {.async: (raises: [Can | |
if id == d.localNode.id: | ||
return Opt.some(d.localNode) | ||
|
||
# No point in trying to resolve a banned node because it won't exist in the | ||
# routing table and it will be filtered out of any respones in the lookup call | ||
if d.isBanned(id): | ||
debug "Not resolving banned node", nodeId = id | ||
return Opt.none(Node) | ||
|
||
let node = d.getNode(id) | ||
if node.isSome(): | ||
let request = await d.findNode(node.get(), @[0'u16]) | ||
|
@@ -882,6 +934,9 @@ proc refreshLoop(d: Protocol) {.async: (raises: []).} = | |
trace "Discovered nodes in random target query", nodes = randomQuery.len | ||
debug "Total nodes in discv5 routing table", total = d.routingTable.len() | ||
|
||
# Remove the expired bans from routing table to limit memory usage | ||
d.routingTable.cleanupExpiredBans() | ||
|
||
await sleepAsync(refreshInterval) | ||
except CancelledError: | ||
trace "refreshLoop canceled" | ||
|
@@ -985,6 +1040,7 @@ proc newProtocol*( | |
bindPort: Port, | ||
bindIp = IPv4_any(), | ||
enrAutoUpdate = false, | ||
banNodes = false, | ||
config = defaultDiscoveryConfig, | ||
rng = newRng()): | ||
Protocol = | ||
|
@@ -1034,6 +1090,7 @@ proc newProtocol*( | |
enrAutoUpdate: enrAutoUpdate, | ||
routingTable: RoutingTable.init( | ||
node, config.bitsPerHop, config.tableIpLimits, rng), | ||
banNodes: banNodes, | ||
handshakeTimeout: config.handshakeTimeout, | ||
responseTimeout: config.responseTimeout, | ||
rng: rng) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An improvement for the future here could be to ban without actually doing the decryption of the message (only the header). But they way the
decodePacket
call is currently designed this is not really possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I guess we could use the src-id in the authdata section of the packet header for ordinary messages and handshake messages.