Skip to content

Commit 609eb31

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 38eac8c commit 609eb31

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lightning/src/ln/msgs.rs

+9
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ mod fuzzy_internal_msgs {
620620
#[derive(Clone)]
621621
pub(crate) struct FinalOnionHopData {
622622
pub(crate) payment_secret: [u8; 32],
623+
/// The total value, in msat, of the payment as received by the ultimate recipient.
624+
/// Message serialization may panic if this value is more than 21 million Bitcoin.
623625
pub(crate) total_msat: u64,
624626
}
625627

@@ -637,6 +639,8 @@ mod fuzzy_internal_msgs {
637639

638640
pub struct OnionHopData {
639641
pub(crate) format: OnionHopDataFormat,
642+
/// The value, in msat, of the payment after this hop's fee is deducted.
643+
/// Message serialization may panic if this value is more than 21 million Bitcoin.
640644
pub(crate) amt_to_forward: u64,
641645
pub(crate) outgoing_cltv_value: u32,
642646
// 12 bytes of 0-padding for Legacy format
@@ -994,6 +998,10 @@ impl Readable for FinalOnionHopData {
994998
impl Writeable for OnionHopData {
995999
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
9961000
w.size_hint(33);
1001+
// Note that this should never be reachable if Rust-Lightning generated the message, as we
1002+
// check values are sane long before we get here, though its possible in the future
1003+
// user-generated messages may hit this.
1004+
if self.amt_to_forward > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
9971005
match self.format {
9981006
OnionHopDataFormat::Legacy { short_channel_id } => {
9991007
0u8.write(w)?;
@@ -1011,6 +1019,7 @@ impl Writeable for OnionHopData {
10111019
},
10121020
OnionHopDataFormat::FinalNode { ref payment_data } => {
10131021
if let &Some(ref final_data) = payment_data {
1022+
if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
10141023
encode_varint_length_prefixed_tlv!(w, {
10151024
(2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
10161025
(4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)),

0 commit comments

Comments
 (0)