support: clamp RLIMIT_MEMLOCK to size_t
What changed, and why it matters
This change fixes a type-size mismatch on 32-bit systems. On those systems, the operating system can report a memory-lock limit as a 64-bit number, but the program stores it in a 32-bit variable. Without the fix, a very large limit could wrap around to a tiny value, potentially causing the wallet to lock far less memory than intended and possibly mishandle sensitive key data. The patch clamps the value to the largest safe 32-bit size before using it.
Backport to supported 32-bit release branches and verify no other rlim_t-to-size_t conversions exist in the locked-pool or memory-locking code paths.
Security signals we found
Integer truncation / type narrowing on 32-bit builds
Potential undersized locked memory allocation
Sensitive-data handling path (locked pool for cryptographic keys)
No explicit security framing in commit message
Evidence from the diff
Bitcoin Core builds 32-bit targets with _FILE_OFFSET_BITS=64, which makes glibc’s rlim_t 64-bit while size_t remains 32-bit. PosixLockedPageAllocator::GetLimit() returns size_t but previously returned rlim.rlim_cur directly. If rlim_cur exceeded SIZE_MAX, the implicit conversion truncated the value. The patch adds std::cmp_less_equal against std::numeric_limits
Changed components
src/support/lockedpool.cppPosixLockedPageAllocator::GetLimit()32-bit glibc buildsInspect captured patch +2 / −1
diff --git a/src/support/lockedpool.cpp b/src/support/lockedpool.cpp
index ff3a9e69..97f0df40 100644
--- a/src/support/lockedpool.cpp
+++ b/src/support/lockedpool.cpp
@@ -262,7 +262,8 @@ size_t PosixLockedPageAllocator::GetLimit()
#ifdef RLIMIT_MEMLOCK
struct rlimit rlim;
if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
- if (rlim.rlim_cur != RLIM_INFINITY) {
+ if (rlim.rlim_cur != RLIM_INFINITY &&
+ std::cmp_less_equal(rlim.rlim_cur, static_cast<rlim_t>(std::numeric_limits<size_t>::max()))) {
return rlim.rlim_cur;
}
}
Why this scored 43/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.