From c186f54edcdf8c00d35fd96534297288e26f6ba6 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Thu, 8 Oct 2026 06:52:11 +0200 Subject: [PATCH] csdb: bound Pool reserve() by the bytes left to parse Pool::priv::getTransactions() and getNewWallets() reserve() as many elements as the uint32 count read from the data. A peer-supplied block (e.g. a BlockReply while syncing) with a count near 2^32 asks for tens of GB: std::bad_alloc/length_error escapes the transport thread and the node terminates. Reserve at most the number of bytes still unread, because every element takes at least one of them. For valid data this reserves exactly what it did before. For forged data the parse loop fails at the first missing element, as it already did, and the pool is invalid. The other counts in Pool parsing are uint8, so they are left as they are. Tests (tests/csnode/pool_tests.cpp): - Pool.HugeTransactionsCountDoesNotAllocate: before the fix from_binary throws. - Pool.HugeNewWalletsCountDoesNotAllocate: before the fix from_binary throws. nodetests: 116/116 pass. Co-Authored-By: Claude Opus 5.5 (1M context) --- csdb/src/pool.cpp | 6 +++-- tests/csnode/pool_tests.cpp | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/csdb/src/pool.cpp b/csdb/src/pool.cpp index 58116d6bd..34132734f 100644 --- a/csdb/src/pool.cpp +++ b/csdb/src/pool.cpp @@ -258,7 +258,8 @@ class Pool::priv : public ::csdb::internal::shared_data { bool getTransactions(::csdb::priv::ibstream& is, size_t cnt) { transactions_.clear(); - transactions_.reserve(cnt); + // cnt comes from the data: every transaction takes at least one more byte of them + transactions_.reserve(std::min(cnt, is.size())); for (size_t i = 0; i < cnt; ++i) { Transaction tran; if (!is.get(tran)) { @@ -382,7 +383,8 @@ class Pool::priv : public ::csdb::internal::shared_data { } newWallets_.clear(); - newWallets_.reserve(cnt); + // cnt comes from the data: every wallet takes at least one more byte of them + newWallets_.reserve(std::min(static_cast(cnt), is.size())); for (uint32_t i = 0; i < cnt; ++i) { NewWalletInfo wall; if (!is.get(wall)) { diff --git a/tests/csnode/pool_tests.cpp b/tests/csnode/pool_tests.cpp index f3127ce8d..7f1d451a7 100644 --- a/tests/csnode/pool_tests.cpp +++ b/tests/csnode/pool_tests.cpp @@ -2,7 +2,9 @@ #include "gtest/gtest.h" +#include #include +#include #include @@ -38,3 +40,52 @@ TEST(Pool, RejectsHashingLengthPastData) { ASSERT_FALSE(csdb::Pool::from_binary(cs::Bytes(bytes)).is_valid()); ASSERT_TRUE(csdb::Pool::hash_from_binary(cs::Bytes(bytes)).is_empty()); } + +// offset of the transactions count: the only uint32 that makes meta_from_binary() report it +static size_t transactionsCountOffset(const cs::Bytes& bytes) { + const uint32_t marker = std::numeric_limits::max(); + + for (size_t offset = 0; offset + sizeof(marker) <= bytes.size(); ++offset) { + cs::Bytes copy = bytes; + std::memcpy(copy.data() + offset, &marker, sizeof(marker)); + + size_t count = 0; + if (csdb::Pool::meta_from_binary(std::move(copy), count).is_valid() && count == marker) { + return offset; + } + } + + return bytes.size(); +} + +// counts come from the data: a forged one must not turn into a huge allocation +TEST(Pool, HugeTransactionsCountDoesNotAllocate) { + size_t hashingLength = 0; + cs::Bytes bytes = poolBinary(hashingLength); + + const size_t offset = transactionsCountOffset(bytes); + ASSERT_LT(offset, bytes.size()); + + const uint32_t forged = std::numeric_limits::max(); + std::memcpy(bytes.data() + offset, &forged, sizeof(forged)); + + csdb::Pool pool; + ASSERT_NO_THROW(pool = csdb::Pool::from_binary(std::move(bytes))); + ASSERT_FALSE(pool.is_valid()); +} + +// with no transactions, the new wallets count follows the transactions count +TEST(Pool, HugeNewWalletsCountDoesNotAllocate) { + size_t hashingLength = 0; + cs::Bytes bytes = poolBinary(hashingLength); + + const size_t offset = transactionsCountOffset(bytes) + sizeof(uint32_t); + ASSERT_LE(offset + sizeof(uint32_t), bytes.size()); + + const uint32_t forged = std::numeric_limits::max(); + std::memcpy(bytes.data() + offset, &forged, sizeof(forged)); + + csdb::Pool pool; + ASSERT_NO_THROW(pool = csdb::Pool::from_binary(std::move(bytes))); + ASSERT_FALSE(pool.is_valid()); +}