cryptonote_basic: sanity check on key offsets
What changed, and why it matters
This commit adds an early safety check to Monero's transaction parsing code. It rejects transactions that contain an impossibly large number of 'key offsets'—the references that identify which previous transaction outputs a transaction is spending from. The limit is based on the maximum allowed transaction size, so any transaction exceeding it cannot be valid anyway. The change is defensive and prevents malformed or oversized transaction data from being processed further.
Treat as a defensive hardening commit. Review whether the chosen ceiling is tight enough for all consensus paths and ensure the new check is also applied to any alternate transaction parsing paths (e.g., RPC, mempool, block validation) that may not route through these four functions. No immediate incident response is indicated absent a disclosed vulnerability.
Security signals we found
New input-validation/sanity-check on deserialized transaction data
Integer overflow guard on cumulative key offset count
Early rejection of oversized/malformed transaction structures
Defensive hardening rather than explicit bug fix
Evidence from the diff
The patch introduces max_total_key_offsets() and n_key_offsets_exceeds_max() in cryptonote_format_utils.cpp. The helper computes a hard upper bound on total key offsets as get_max_tx_size() / sizeof(crypto::public_key), then sums key_offsets across all txin_to_key inputs, checking for addition overflow. It is invoked immediately after deserialization in four parsing entry points: parse_and_validate_tx_from_blob, parse_tx_from_blob, parse_tx_prefix_from_blob, and another parse_and_validate_tx_from_blob overload. The intent is to fail fast on transactions whose ring-member references exceed what could fit in a maximally sized transaction.
Changed components
src/cryptonote_basic/cryptonote_format_utils.cppTransaction deserialization/parsing functionstxin_to_key input handlingInspect captured patch +25 / −0
diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp
index 09ec8ff..ddc97c5 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.cpp
+++ b/src/cryptonote_basic/cryptonote_format_utils.cpp
@@ -99,6 +99,27 @@ namespace cryptonote
const uint64_t bp_clawback = (bp_base * n_padded_outputs - bp_size) * 4 / 5;
return bp_clawback;
}
+
+ std::size_t max_total_key_offsets()
+ {
+ // This is a 100% guaranteed ceiling for the entire chain
+ return get_max_tx_size() / sizeof(crypto::public_key);
+ }
+
+ bool n_key_offsets_exceeds_max(const transaction_prefix& tx)
+ {
+ const std::size_t max_allowed = max_total_key_offsets();
+ std::size_t total_key_offsets = 0;
+ for (const auto &vin : tx.vin)
+ {
+ if (vin.type() != typeid(cryptonote::txin_to_key))
+ continue;
+ const std::size_t n_key_offsets = boost::get<cryptonote::txin_to_key>(vin).key_offsets.size();
+ CHECK_AND_ASSERT_MES((n_key_offsets + total_key_offsets) >= total_key_offsets, true, "key offsets overflow");
+ total_key_offsets += n_key_offsets;
+ }
+ return total_key_offsets >= max_allowed;
+ }
//---------------------------------------------------------------
}
@@ -203,6 +224,7 @@ namespace cryptonote
binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
bool r = ::serialization::serialize(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
+ CHECK_AND_ASSERT_MES(!n_key_offsets_exceeds_max(tx), false, "Transaction contains too many ring members");
CHECK_AND_ASSERT_MES(expand_transaction_1(tx, false), false, "Failed to expand transaction data");
tx.invalidate_hashes();
tx.set_blob_size(tx_blob.size());
@@ -214,6 +236,7 @@ namespace cryptonote
binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
bool r = tx.serialize_base(ba);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
+ CHECK_AND_ASSERT_MES(!n_key_offsets_exceeds_max(tx), false, "Transaction contains too many ring members");
CHECK_AND_ASSERT_MES(expand_transaction_1(tx, true), false, "Failed to expand transaction data");
tx.invalidate_hashes();
return true;
@@ -224,6 +247,7 @@ namespace cryptonote
binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
bool r = ::serialization::serialize_noeof(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction prefix from blob");
+ CHECK_AND_ASSERT_MES(!n_key_offsets_exceeds_max(tx), false, "Transaction contains too many ring members");
return true;
}
//---------------------------------------------------------------
@@ -232,6 +256,7 @@ namespace cryptonote
binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
bool r = ::serialization::serialize(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
+ CHECK_AND_ASSERT_MES(!n_key_offsets_exceeds_max(tx), false, "Transaction contains too many ring members");
CHECK_AND_ASSERT_MES(expand_transaction_1(tx, false), false, "Failed to expand transaction data");
tx.invalidate_hashes();
tx.set_blob_size(tx_blob.size());
Why this scored 59/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.