Skip to content

Commit d3b6083

Browse files
committed
panic!() when serializing OnionHopDatas with value > 21m BTC
Add documentation to the struct fields noting this to avoid missing docs when various msg structs become public.
1 parent 03a1b52 commit d3b6083

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lightning/src/ln/msgs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ mod fuzzy_internal_msgs {
622622
#[derive(Clone)]
623623
pub(crate) struct FinalOnionHopData {
624624
pub(crate) payment_secret: PaymentSecret,
625+
/// The total value, in msat, of the payment as received by the ultimate recipient.
626+
/// Message serialization may panic if this value is more than 21 million Bitcoin.
625627
pub(crate) total_msat: u64,
626628
}
627629

@@ -639,6 +641,8 @@ mod fuzzy_internal_msgs {
639641

640642
pub struct OnionHopData {
641643
pub(crate) format: OnionHopDataFormat,
644+
/// The value, in msat, of the payment after this hop's fee is deducted.
645+
/// Message serialization may panic if this value is more than 21 million Bitcoin.
642646
pub(crate) amt_to_forward: u64,
643647
pub(crate) outgoing_cltv_value: u32,
644648
// 12 bytes of 0-padding for Legacy format
@@ -996,6 +1000,10 @@ impl Readable for FinalOnionHopData {
9961000
impl Writeable for OnionHopData {
9971001
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
9981002
w.size_hint(33);
1003+
// Note that this should never be reachable if Rust-Lightning generated the message, as we
1004+
// check values are sane long before we get here, though its possible in the future
1005+
// user-generated messages may hit this.
1006+
if self.amt_to_forward > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
9991007
match self.format {
10001008
OnionHopDataFormat::Legacy { short_channel_id } => {
10011009
0u8.write(w)?;
@@ -1012,6 +1020,7 @@ impl Writeable for OnionHopData {
10121020
});
10131021
},
10141022
OnionHopDataFormat::FinalNode { payment_data: Some(ref final_data) } => {
1023+
if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
10151024
encode_varint_length_prefixed_tlv!(w, {
10161025
(2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
10171026
(4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)),

0 commit comments

Comments
 (0)