Skip to content

Commit

Permalink
fix: ban header sync peer if no headers provided (#3297)
Browse files Browse the repository at this point in the history
Description
---
Adds ban in header sync if peer has indicated that they have a better chain but is unable to provide headers 

Motivation and Context
---
A peer can advertise a higher chain metadata and then return no
headers. This case is likely malicious. In this PR, this is handled
by banning the peer allowing the node to attempt a sync with another peer.

How Has This Been Tested?
---
Manually
  • Loading branch information
sdbondi committed Sep 6, 2021
1 parent 12cb1fb commit 570e222
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base_layer/core/src/base_node/sync/header_sync/error.rs
Expand Up @@ -63,4 +63,6 @@ pub enum BlockHeaderSyncError {
InvalidProtocolResponse(String),
#[error("Headers did not form a chain. Expected {actual} to equal the previous hash {expected}")]
ChainLinkBroken { actual: String, expected: String },
#[error("Invalid remote peer behaviour: {0}")]
InvalidRemotePeerBehaviour(String),
}
14 changes: 14 additions & 0 deletions base_layer/core/src/base_node/sync/header_sync/synchronizer.rs
Expand Up @@ -127,6 +127,11 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
warn!(target: LOG_TARGET, "Chain split not found for peer {}.", peer);
self.ban_peer_long(peer, BanReason::ChainSplitNotFound).await?;
},
Err(err @ BlockHeaderSyncError::InvalidRemotePeerBehaviour(_)) => {
warn!(target: LOG_TARGET, "{}", err);
self.ban_peer_long(node_id, BanReason::PeerMisbehaviour(err.to_string()))
.await?;
},
Err(err @ BlockHeaderSyncError::InvalidBlockHeight { .. }) => {
warn!(target: LOG_TARGET, "{}", err);
self.ban_peer_long(node_id, BanReason::GeneralHeaderSyncFailure(err))
Expand Down Expand Up @@ -382,6 +387,13 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
peer
);

if headers.is_empty() {
return Err(BlockHeaderSyncError::InvalidRemotePeerBehaviour(format!(
"Remote peer {} advertised a better chain but did not return headers",
peer
)));
}

if fork_hash_index >= block_hashes.len() as u32 {
let _ = self
.ban_peer_long(peer.clone(), BanReason::SplitHashGreaterThanHashes {
Expand Down Expand Up @@ -677,6 +689,8 @@ enum BanReason {
GeneralHeaderSyncFailure(BlockHeaderSyncError),
#[error("Peer did not respond timeously during RPC negotiation")]
RpcNegotiationTimedOut,
#[error("Peer misbehaviour: {0}")]
PeerMisbehaviour(String),
}

struct ChainSplitInfo {
Expand Down

0 comments on commit 570e222

Please sign in to comment.