Skip to content

Commit 7b9bde1

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
1 parent 0eec30a commit 7b9bde1

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
@@ -1688,7 +1688,14 @@ impl Readable for Duration {
16881688
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
16891689
let secs = Readable::read(r)?;
16901690
let nanos = Readable::read(r)?;
1691-
Ok(Duration::new(secs, nanos))
1691+
// Duration::new panics if the nanosecond part in excess of a second, added to the second
1692+
// part, overflows. To ensure this won't happen, we simply reject any case where there are
1693+
// nanoseconds in excess of a second, which is invalid anyway.
1694+
if nanos >= 1_000_000_000 {
1695+
Err(DecodeError::InvalidValue)
1696+
} else {
1697+
Ok(Duration::new(secs, nanos))
1698+
}
16921699
}
16931700
}
16941701

0 commit comments

Comments
 (0)