Skip to content

Commit 82d3058

Browse files
committed
cuckoocache: Check for uint32 overflow in setup_bytes
This fixes an potential overflow which existed prior to this patchset. If CuckooCache::cache<Element, Hash>::setup_bytes is called with a `size_t bytes` which, when divided by sizeof(Element), does not fit into an uint32_t, the implicit conversion to uint32_t in the call to setup will result in an overflow. At least on x86_64, this overflow is possible: static_assert(std::numeric_limits<size_t>::max() / 32 <= std::numeric_limits<uint32_t>::max()); static_assert(std::numeric_limits<size_t>::max() / 4 <= std::numeric_limits<uint32_t>::max()); This commit detects such cases and signals to callers that the `size_t bytes` input is too large.
1 parent b370164 commit 82d3058

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/cuckoocache.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include <atomic>
1313
#include <cmath>
1414
#include <cstring>
15+
#include <limits>
1516
#include <memory>
17+
#include <optional>
1618
#include <utility>
1719
#include <vector>
1820

@@ -359,10 +361,15 @@ class cache
359361
* structure
360362
* @returns A pair of the maximum number of elements storable (see setup()
361363
* documentation for more detail) and the approxmiate total size of these
362-
* elements in bytes.
364+
* elements in bytes or std::nullopt if the size requested is too large.
363365
*/
364-
std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
366+
std::optional<std::pair<uint32_t, size_t>> setup_bytes(size_t bytes)
365367
{
368+
size_t requested_num_elems = bytes / sizeof(Element);
369+
if (std::numeric_limits<uint32_t>::max() < requested_num_elems) {
370+
return std::nullopt;
371+
}
372+
366373
auto num_elems = setup(bytes/sizeof(Element));
367374

368375
size_t approx_size_bytes = num_elems * sizeof(Element);

src/script/sigcache.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <algorithm>
1616
#include <mutex>
17+
#include <optional>
1718
#include <shared_mutex>
1819
#include <vector>
1920

@@ -75,7 +76,7 @@ class CSignatureCache
7576
std::unique_lock<std::shared_mutex> lock(cs_sigcache);
7677
setValid.insert(entry);
7778
}
78-
std::pair<uint32_t, size_t> setup_bytes(size_t n)
79+
std::optional<std::pair<uint32_t, size_t>> setup_bytes(size_t n)
7980
{
8081
return setValid.setup_bytes(n);
8182
}
@@ -99,8 +100,9 @@ bool InitSignatureCache()
99100
size_t nMaxCacheSize = std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2) * ((size_t) 1 << 20);
100101

101102
auto setup_results = signatureCache.setup_bytes(nMaxCacheSize);
103+
if (!setup_results) return false;
102104

103-
const auto [num_elems, approx_size_bytes] = setup_results;
105+
const auto [num_elems, approx_size_bytes] = *setup_results;
104106
LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
105107
approx_size_bytes >> 20, (nMaxCacheSize * 2) >> 20, num_elems);
106108
return true;

src/validation.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1669,8 +1669,9 @@ bool InitScriptExecutionCache() {
16691669
size_t nMaxCacheSize = std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2) * ((size_t) 1 << 20);
16701670

16711671
auto setup_results = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
1672+
if (!setup_results) return false;
16721673

1673-
const auto [num_elems, approx_size_bytes] = setup_results;
1674+
const auto [num_elems, approx_size_bytes] = *setup_results;
16741675
LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
16751676
approx_size_bytes >> 20, (nMaxCacheSize * 2) >> 20, num_elems);
16761677
return true;

0 commit comments

Comments
 (0)