What changed, and why it matters
This Monero update tightens how a node checks block-download requests it has 'borrowed' from another peer. Before serving a borrowed range of blocks, the node now verifies that range against the peer's own known history. If the history is missing or doesn't match, the node either refreshes its chain knowledge or puts the peer on standby instead of blindly trusting the borrowed blocks. This reduces the chance that a malicious or inconsistent peer can feed a node the wrong chain during sync.
Treat as a hardening fix for the P2P sync protocol. Nodes and services running Monero should upgrade to include this commit. No immediate emergency response is indicated, but the change addresses a real integrity issue during sync from untrusted peers.
Security signals we found
New validation of borrowed block spans against peer expected hashes
Refusal to request blocks when span history is missing or on a different chain
Addition of state flag to coordinate chain refresh with next span download
Defense against stale or inconsistent peer history during blockchain sync
Evidence from the diff
The patch adds a m_refresh_for_next_span flag and a match_span helper that classifies a scheduled block span as matching, missing history, or belonging to a different chain. When a span is borrowed from another peer, the protocol handler now checks it against context.get_expected_hash() before requesting blocks. Missing-history spans trigger a chain refresh (request_chain(true)), while mismatched spans cause the peer to be set to state_standby. The old inline chain-request code is refactored into a reusable request_chain lambda. A refresh loop is avoided by only forcing the next span when the refreshed history actually covers it.
Changed components
src/cryptonote_protocol/cryptonote_protocol_handler.inlsrc/cryptonote_basic/connection_context.hP2P block synchronization / span borrowing logicInspect captured patch +104 / −29
### src/cryptonote_basic/connection_context.h
@@ -107,6 +107,7 @@ namespace cryptonote
boost::posix_time::ptime m_last_request_time;
copyable_atomic m_callback_request_count; //in debug purpose: problem with double callback rise
crypto::hash m_last_known_hash;
+ bool m_refresh_for_next_span = false;
uint32_t m_pruning_seed;
uint16_t m_rpc_port;
uint32_t m_rpc_credits_per_hash;
### src/cryptonote_protocol/cryptonote_protocol_handler.inl
@@ -181,6 +181,7 @@ namespace cryptonote
MINFO(context << " kicking idle peer, last update " << (dt.total_microseconds() / 1.e6) << " seconds ago, expecting " << (int)context.m_expect_response);
context.m_last_request_time = boost::date_time::not_a_date_time;
context.m_expect_response = 0;
+ context.m_refresh_for_next_span = false;
context.m_expect_height = 0;
context.m_requested_objects.clear();
context.m_state = cryptonote_connection_context::state_standby; // we'll go back to adding, then (if we can't), download
@@ -1965,6 +1966,48 @@ skip:
});
m_block_queue.flush_stale_spans(live_connections);
+ enum class span_match { matches, missing_history, different_chain };
+ const auto match_span = [&context](uint64_t start_height, const std::vector<crypto::hash>& hashes) {
+ // Object validation also needs the predecessor of the first requested block.
+ if (start_height == 0 || hashes.empty() || !context.get_expected_hash(start_height - 1)
+ || !context.get_expected_hash(start_height + hashes.size() - 1))
+ return span_match::missing_history;
+ for (size_t i = 0; i < hashes.size(); ++i)
+ if (context.get_expected_hash(start_height + i) != hashes[i])
+ return span_match::different_chain;
+ return span_match::matches;
+ };
+
+ const auto request_chain = [this, &context](bool refresh) {
+ if (refresh)
+ {
+ // Refresh history from the committed chain.
+ context.m_needed_objects.clear();
+ context.m_last_known_hash = crypto::null_hash;
+ }
+
+ NOTIFY_REQUEST_CHAIN::request r = {};
+ m_core.get_short_chain_history(r.block_ids, context.m_expect_height);
+ CHECK_AND_ASSERT_MES(!r.block_ids.empty(), false, "Short chain history is empty");
+
+ // Continue from this peer's last known block, which may still be queued.
+ if (context.m_last_known_hash != crypto::null_hash && r.block_ids.front() != context.m_last_known_hash)
+ {
+ context.m_expect_height = std::numeric_limits<uint64_t>::max();
+ r.block_ids.push_front(context.m_last_known_hash);
+ }
+
+ handler_request_blocks_history(r.block_ids);
+ r.prune = m_sync_pruned_blocks;
+ context.m_refresh_for_next_span = refresh;
+ context.m_last_request_time = boost::posix_time::microsec_clock::universal_time();
+ context.m_expect_response = NOTIFY_RESPONSE_CHAIN_ENTRY::ID;
+ MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size());
+ post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
+ MLOG_PEER_STATE("requesting chain");
+ return true;
+ };
+
// if we don't need to get next span, and the block queue is full enough, wait a bit
if (!force_next_span)
{
@@ -2133,6 +2176,27 @@ skip:
span = m_block_queue.get_next_span_if_scheduled(hashes, span_connection_id, time);
if (span.second > 0)
{
+ const auto match = match_span(span.first, hashes);
+ if (match == span_match::missing_history)
+ {
+ if (context.m_remote_blockchain_height < span.first + span.second
+ || (!m_sync_pruned_blocks && tools::get_next_pruned_block_height(span.first,
+ context.m_remote_blockchain_height, context.m_pruning_seed) < span.first + span.second))
+ {
+ MDEBUG(context << "Peer cannot serve the full span " << span.first << " - "
+ << (span.first + span.second - 1) << ", going to standby");
+ context.m_state = cryptonote_connection_context::state_standby;
+ return true;
+ }
+ MDEBUG(context << "Missing peer history for span " << span.first << " - " << (span.first + span.second - 1) << ", refreshing chain");
+ return request_chain(true);
+ }
+ if (match == span_match::different_chain)
+ {
+ MDEBUG(context << "Span " << span.first << " - " << (span.first + span.second - 1) << " does not match peer history, going to standby");
+ context.m_state = cryptonote_connection_context::state_standby;
+ return true;
+ }
is_next = true;
req.blocks.reserve(hashes.size());
for (const auto &hash: hashes)
@@ -2194,6 +2258,27 @@ skip:
span = std::make_pair(0, 0);
if (span.second > 0)
{
+ const auto match = match_span(span.first, hashes);
+ if (match == span_match::missing_history)
+ {
+ if (context.m_remote_blockchain_height < span.first + span.second
+ || (!m_sync_pruned_blocks && tools::get_next_pruned_block_height(span.first,
+ context.m_remote_blockchain_height, context.m_pruning_seed) < span.first + span.second))
+ {
+ MDEBUG(context << "Peer cannot serve the full span " << span.first << " - "
+ << (span.first + span.second - 1) << ", going to standby");
+ context.m_state = cryptonote_connection_context::state_standby;
+ return true;
+ }
+ MDEBUG(context << "Missing peer history for span " << span.first << " - " << (span.first + span.second - 1) << ", refreshing chain");
+ return request_chain(true);
+ }
+ if (match == span_match::different_chain)
+ {
+ MDEBUG(context << "Span " << span.first << " - " << (span.first + span.second - 1) << " does not match peer history, going to standby");
+ context.m_state = cryptonote_connection_context::state_standby;
+ return true;
+ }
is_next = true;
req.blocks.reserve(hashes.size());
for (const auto &hash: hashes)
@@ -2310,34 +2395,9 @@ skip:
}
}
- if(context.m_last_response_height < context.m_remote_blockchain_height-1)
- {//we have to fetch more objects ids, request blockchain entry
-
- NOTIFY_REQUEST_CHAIN::request r = {};
- m_core.get_short_chain_history(r.block_ids, context.m_expect_height);
- CHECK_AND_ASSERT_MES(!r.block_ids.empty(), false, "Short chain history is empty");
-
- // we'll want to start off from where we are on that peer, which may not be added yet
- if (context.m_last_known_hash != crypto::null_hash && r.block_ids.front() != context.m_last_known_hash)
- {
- context.m_expect_height = std::numeric_limits<uint64_t>::max();
- r.block_ids.push_front(context.m_last_known_hash);
- }
-
- handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?)
- r.prune = m_sync_pruned_blocks;
-
- //std::string blob; // for calculate size of request
- //epee::serialization::store_t_to_binary(r, blob);
- //epee::net_utils::network_throttle_manager::get_global_throttle_inreq().logger_handle_net("log/dr-monero/net/req-all.data", sec, get_avg_block_size());
- //LOG_PRINT_CCONTEXT_L1("r = " << 200);
-
- context.m_last_request_time = boost::posix_time::microsec_clock::universal_time();
- context.m_expect_response = NOTIFY_RESPONSE_CHAIN_ENTRY::ID;
- MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size());
- post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
- MLOG_PEER_STATE("requesting chain");
- }else
+ if (context.m_last_response_height < context.m_remote_blockchain_height-1)
+ return request_chain(false);
+ else
{
CHECK_AND_ASSERT_MES(context.m_last_response_height == context.m_remote_blockchain_height-1
&& !context.m_needed_objects.size()
@@ -2448,6 +2508,8 @@ skip:
return LEVIN_ERROR_CONNECTION;
}
context.m_expect_response = 0;
+ const bool refresh_for_next_span = context.m_refresh_for_next_span;
+ context.m_refresh_for_next_span = false;
if (arg.start_height + 1 > context.m_expect_height) // we expect an overlapping block
{
LOG_ERROR_CCONTEXT("Got NOTIFY_RESPONSE_CHAIN_ENTRY past expected height, dropping connection");
@@ -2590,7 +2652,19 @@ skip:
}
context.m_last_response_height -= arg.m_block_ids.size() - n_use_blocks;
- if (!request_missing_objects(context, false))
+ bool force_next_span = false;
+ if (refresh_for_next_span && should_download_next_span(context, true))
+ {
+ std::vector<crypto::hash> hashes;
+ boost::uuids::uuid span_connection_id;
+ boost::posix_time::ptime time;
+ const auto span = m_block_queue.get_next_span_if_scheduled(hashes, span_connection_id, time);
+ // Avoid a refresh loop when the reply still does not cover the span.
+ force_next_span = span.first > 0 && !hashes.empty()
+ && context.get_expected_hash(span.first - 1)
+ && context.get_expected_hash(span.first + hashes.size() - 1);
+ }
+ if (!request_missing_objects(context, false, force_next_span))
{
LOG_ERROR_CCONTEXT("Failed to request missing objects, dropping connection");
drop_connection(context, false, false);Why this scored 61/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.