Merge pull request #11235

f8e2a87 cryptonote_basic: serialization checks (j-berman)

ACKs: plowsof, selsta*, jpk68
This commit is contained in:
tobtoht
2026-09-10 13:30:16 +00:00
14 changed files with 136 additions and 66 deletions
+101 -3
View File
@@ -52,6 +52,13 @@
namespace cryptonote
{
// These are guaranteed ceilings for the entire chain. Some fork rules may limit some further.
constexpr size_t COINBASE_VIN_COUNT = 1;
constexpr size_t MAX_VIN_COUNT = CRYPTONOTE_MAX_TX_SIZE / sizeof(crypto::key_image);
constexpr size_t MAX_NON_COINBASE_VOUT_COUNT = CRYPTONOTE_MAX_TX_SIZE / sizeof(crypto::public_key);
constexpr size_t MAX_COINBASE_VOUT_COUNT = /*LEVIN_DEFAULT_MAX_PACKET_SIZE=*/100000000 / sizeof(crypto::public_key);
constexpr size_t MAX_TOTAL_KEY_OFFSETS = CRYPTONOTE_MAX_TX_SIZE / sizeof(crypto::public_key);
/* outputs */
struct txout_to_script
@@ -139,7 +146,7 @@ namespace cryptonote
BEGIN_SERIALIZE_OBJECT()
VARINT_FIELD(amount)
FIELD(key_offsets)
CONTAINER_FIELD_CAPPED(key_offsets, MAX_TOTAL_KEY_OFFSETS)
FIELD(k_image)
END_SERIALIZE()
};
@@ -179,11 +186,102 @@ namespace cryptonote
VARINT_FIELD(version)
if((version == 0 || CURRENT_TRANSACTION_VERSION < version)) return false;
VARINT_FIELD(unlock_time)
FIELD(vin)
FIELD(vout)
if constexpr (W)
{
// Serializing...
FIELD(vin)
FIELD(vout)
}
else
{
// De-serializing...
// vin
if (!this->deserialize_vin(ar))
{
ar.set_fail();
return false;
}
// vout
const size_t MAX_VOUT_COUNT = (this->is_coinbase() || vin.empty()) ? MAX_COINBASE_VOUT_COUNT : MAX_NON_COINBASE_VOUT_COUNT;
CONTAINER_FIELD_CAPPED(vout, MAX_VOUT_COUNT)
}
FIELD(extra)
END_SERIALIZE()
//-----------------------------------------------------------------------
bool is_coinbase() const
{
if (vin.size() != COINBASE_VIN_COUNT)
return false;
if (vin.at(0).type() != typeid(txin_gen))
return false;
return true;
}
//-----------------------------------------------------------------------
private:
// Do some validation on vin before doing allocations and reading the archive:
// 1. Can't exceed MAX_VIN_COUNT.
// 2. If coinbase, only 1 txin_gen.
// 3. If not coinbase: type txin_to_key (see check_inputs_types_supported).
// 4. If not coinbase: key offsets don't exceed max allowed for whole tx.
// Note: can't enforce non-empty because wallets may store empty vin.
template<template <bool> class Archive>
bool deserialize_vin(Archive<false> &ar)
{
auto &v = vin;
size_t cnt = 0;
ar.begin_array(cnt);
if (!ar.good())
return false;
v.clear();
if (ar.remaining_bytes() < cnt)
return false;
// 1. Can't exceed MAX_VIN_COUNT.
if (cnt > MAX_VIN_COUNT)
return false;
::serialization::detail::do_reserve(v, cnt, ar.remaining_bytes());
size_t total_key_offsets = 0;
for (size_t i = 0; i < cnt; ++i) {
if (i > 0)
ar.delimit_array();
if (!::serialization::detail::serialize_container_element(ar, v.emplace_back()))
return false;
if (!ar.good())
return false;
// 2. If coinbase, only 1 txin_gen.
const auto &in = v.back();
const bool coinbase = i == 0 && in.type() == typeid(txin_gen);
if (coinbase && cnt > COINBASE_VIN_COUNT)
return false;
if (coinbase)
break;
// 3. If not coinbase: type txin_to_key (see check_inputs_types_supported).
if (in.type() != typeid(txin_to_key))
return false;
// 4. If not coinbase: key offsets don't exceed max allowed for whole tx.
const std::size_t next_toal_key_offsets = total_key_offsets + boost::get<cryptonote::txin_to_key>(in).key_offsets.size();
if (next_toal_key_offsets < total_key_offsets)
return false; // overflow
if (next_toal_key_offsets >= MAX_TOTAL_KEY_OFFSETS)
return false;
total_key_offsets = next_toal_key_offsets;
}
ar.end_array();
v.shrink_to_fit();
return ar.good();
}
public:
transaction_prefix(){ set_null(); }
void set_null()
@@ -150,17 +150,6 @@ namespace cryptonote {
return tools::base58::encode_addr(integrated_address_prefix, t_serializable_object_to_blob(iadr));
}
//-----------------------------------------------------------------------
bool is_coinbase(const transaction_prefix& tx)
{
if(tx.vin.size() != 1)
return false;
if(tx.vin[0].type() != typeid(txin_gen))
return false;
return true;
}
//-----------------------------------------------------------------------
bool get_account_address_from_str(
address_parse_info& info
, network_type nettype
@@ -98,8 +98,6 @@ namespace cryptonote {
, std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm = return_first_address
);
bool is_coinbase(const transaction_prefix& tx);
bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b);
bool operator ==(const cryptonote::block& a, const cryptonote::block& b);
@@ -97,27 +97,6 @@ namespace cryptonote
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;
}
bool passes_max_size_check(const bool max_size_check, const blobdata_ref &tx_blob)
{
if (!max_size_check)
@@ -145,7 +124,7 @@ namespace cryptonote
bool expand_transaction_1(transaction &tx, bool base_only)
{
if (tx.version >= 2 && !is_coinbase(tx))
if (tx.version >= 2 && !tx.is_coinbase())
{
rct::rctSig &rv = tx.rct_signatures;
if (rv.type == rct::RCTTypeNull)
@@ -229,7 +208,6 @@ 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());
@@ -242,7 +220,6 @@ 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;
@@ -254,7 +231,6 @@ 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;
}
//---------------------------------------------------------------
@@ -264,7 +240,6 @@ 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());