Merge PR 'Fix 32-bit spin in Vec<u8> deserialization on huge lengths' (#5102)
What changed, and why it matters
This commit fixes a bug in how rust-lightning reads variable-length byte blobs on 32-bit systems. A malicious message could claim a huge byte count; on 32-bit builds the old code would try to allocate an oversized buffer in tiny increments, likely looping or exhausting memory. The fix caps the per-read chunk size using a 64-bit comparison before converting to the machine's native pointer size, preventing the runaway allocation.
Treat as a low-to-moderate denial-of-service hardening fix. Backport to maintained branches that support 32-bit builds, and include in release notes. No immediate emergency response is warranted unless 32-bit nodes are a supported deployment target.
Security signals we found
Integer truncation / cast-down bug in deserialization
Potential memory exhaustion / denial-of-service on 32-bit targets
Fix caps read chunk size before usize conversion
No explicit CVE or advisory language in commit
Evidence from the diff
In lightning/src/util/ser.rs, the Vec<u8> deserialization loop previously computed cmp::min(len.0 as usize, MAX_BUF_SIZE). On 32-bit targets, len.0 (a u64) could be truncated when cast to usize, producing a value smaller than MAX_BUF_SIZE and causing the loop to spin many times for a single huge claimed length. The patch changes the comparison to cmp::min(len.0, MAX_BUF_SIZE as u64) as usize, so the chunk size is bounded by MAX_BUF_SIZE before the cast. This avoids pathological allocation behavior and potential memory exhaustion on 32-bit platforms.
Changed components
lightning/src/util/ser.rsVec<u8> Readable implementation32-bit builds of rust-lightningInspect captured patch +1 / −1
### lightning/src/util/ser.rs
@@ -1108,7 +1108,7 @@ impl Readable for Vec<u8> {
let mut len: CollectionLength = Readable::read(r)?;
let mut ret = Vec::new();
while len.0 > 0 {
- let readamt = cmp::min(len.0 as usize, MAX_BUF_SIZE);
+ let readamt = cmp::min(len.0, MAX_BUF_SIZE as u64) as usize;
let readstart = ret.len();
ret.resize(readstart + readamt, 0);
r.read_exact(&mut ret[readstart..])?;Why this scored 48/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.