Skip to content

Commit 2efb009

Browse files
authored
Merge pull request #4144 from TheBlueMatt/2025-10-om-read-loop
Correct EOF handling in stream read in `ChaChaDualPolyReadAdapter`
2 parents 7439528 + 423844d commit 2efb009

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lightning/src/crypto/streams.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ impl<T: Readable> LengthReadableArgs<([u8; 32], [u8; 32])> for ChaChaDualPolyRea
130130
let readable: T = Readable::read(&mut chacha_stream)?;
131131
while chacha_stream.read.bytes_remain() {
132132
let mut buf = [0; 256];
133-
chacha_stream.read(&mut buf)?;
133+
if chacha_stream.read(&mut buf)? == 0 {
134+
// Reached EOF
135+
return Err(DecodeError::ShortRead);
136+
}
134137
}
135138

136139
let read_len = chacha_stream.read_len;
@@ -344,4 +347,20 @@ mod tests {
344347
// This also serves to test the `option: $trait` variant of the `_decode_tlv` ser macro.
345348
do_chacha_stream_adapters_ser_macros().unwrap()
346349
}
350+
351+
#[test]
352+
fn short_read_chacha_dual_read_adapter() {
353+
// Previously, if we attempted to read from a ChaChaDualPolyReadAdapter but the object
354+
// being read is shorter than the available buffer while the buffer passed to
355+
// ChaChaDualPolyReadAdapter itself always thinks it has room, we'd end up
356+
// infinite-looping as we didn't handle `Read::read`'s 0 return values at EOF.
357+
let mut stream = &[0; 1024][..];
358+
let mut too_long_stream = FixedLengthReader::new(&mut stream, 2048);
359+
let keys = ([42; 32], [99; 32]);
360+
let res = super::ChaChaDualPolyReadAdapter::<u8>::read(&mut too_long_stream, keys);
361+
match res {
362+
Ok(_) => panic!(),
363+
Err(e) => assert_eq!(e, DecodeError::ShortRead),
364+
}
365+
}
347366
}

0 commit comments

Comments
 (0)