Skip to content

Commit a31227f

Browse files
committed
Fix panic when deserializing Duration
`Duration::new` adds any nanoseconds in excess of a second to the second part. This can overflow, however, panicking. In 0.2 we introduced a few further cases where we store `Duration`s, specifically some when handling network messages. Sadly, that introduced a remotely-triggerable crash where someone can send us, for example, a malicious blinded path context which can cause us to panic. Found by the `onion_message` fuzzer Backport of 7b9bde1
1 parent 4c1aa13 commit a31227f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lightning/src/util/ser.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,14 @@ impl Readable for Duration {
15661566
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
15671567
let secs = Readable::read(r)?;
15681568
let nanos = Readable::read(r)?;
1569-
Ok(Duration::new(secs, nanos))
1569+
// Duration::new panics if the nanosecond part in excess of a second, added to the second
1570+
// part, overflows. To ensure this won't happen, we simply reject any case where there are
1571+
// nanoseconds in excess of a second, which is invalid anyway.
1572+
if nanos >= 1_000_000_000 {
1573+
Err(DecodeError::InvalidValue)
1574+
} else {
1575+
Ok(Duration::new(secs, nanos))
1576+
}
15701577
}
15711578
}
15721579

0 commit comments

Comments
 (0)