Merge pull request #1324 from valentinewallace/2022-02-phantom-followup
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Mon, 28 Feb 2022 18:16:21 +0000 (18:16 +0000)
committerGitHub <noreply@github.com>
Mon, 28 Feb 2022 18:16:21 +0000 (18:16 +0000)
#1199 Followup

1  2 
lightning/src/ln/channelmanager.rs
lightning/src/ln/msgs.rs

index b47caa6aec42d10b54e2526c64c64699b6a11dd7,14e47e71d4b321fa30f2aa2b1f5c6ebd0018243a..674977faaebc2ecd519682ea8f5edb17589fc81b
@@@ -358,7 -358,7 +358,7 @@@ mod inbound_payment 
  // our payment, which we can use to decode errors or inform the user that the payment was sent.
  
  #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
- enum PendingHTLCRouting {
pub(super) enum PendingHTLCRouting {
        Forward {
                onion_packet: msgs::OnionPacket,
                short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
        Receive {
                payment_data: msgs::FinalOnionHopData,
                incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
+               phantom_shared_secret: Option<[u8; 32]>,
        },
        ReceiveKeysend {
                payment_preimage: PaymentPreimage,
  
  #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
  pub(super) struct PendingHTLCInfo {
-       routing: PendingHTLCRouting,
-       incoming_shared_secret: [u8; 32],
+       pub(super) routing: PendingHTLCRouting,
+       pub(super) incoming_shared_secret: [u8; 32],
        payment_hash: PaymentHash,
        pub(super) amt_to_forward: u64,
        pub(super) outgoing_cltv_value: u32,
@@@ -419,6 -420,7 +420,7 @@@ pub(crate) struct HTLCPreviousHopData 
        short_channel_id: u64,
        htlc_id: u64,
        incoming_packet_shared_secret: [u8; 32],
+       phantom_shared_secret: Option<[u8; 32]>,
  
        // This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
        // channel with a preimage provided by the forward channel.
@@@ -1788,7 -1790,7 +1790,7 @@@ impl<Signer: Sign, M: Deref, T: Deref, 
                let mut channel_state = self.channel_state.lock().unwrap();
                match channel_state.by_id.entry(temporary_channel_id) {
                        hash_map::Entry::Occupied(_) => {
 -                              if cfg!(feature = "fuzztarget") {
 +                              if cfg!(fuzzing) {
                                        return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
                                } else {
                                        panic!("RNG is bad???");
        }
  
        fn construct_recv_pending_htlc_info(&self, hop_data: msgs::OnionHopData, shared_secret: [u8; 32],
-               payment_hash: PaymentHash, amt_msat: u64, cltv_expiry: u32) -> Result<PendingHTLCInfo, ReceiveError>
+               payment_hash: PaymentHash, amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>) -> Result<PendingHTLCInfo, ReceiveError>
        {
                // final_incorrect_cltv_expiry
                if hop_data.outgoing_cltv_value != cltv_expiry {
                                        PendingHTLCRouting::Receive {
                                                payment_data: data,
                                                incoming_cltv_expiry: hop_data.outgoing_cltv_value,
+                                               phantom_shared_secret,
                                        }
                                } else if let Some(payment_preimage) = keysend_preimage {
                                        // We need to check that the sender knows the keysend preimage before processing this
                let pending_forward_info = match next_hop {
                        onion_utils::Hop::Receive(next_hop_data) => {
                                // OUR PAYMENT!
-                               match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry) {
+                               match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry, None) {
                                        Ok(info) => {
                                                // Note that we could obviously respond immediately with an update_fulfill_htlc
                                                // message, however that would leak that we are the recipient of this payment, so
                                                                                routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
                                                                                prev_funding_outpoint } => {
                                                                                        macro_rules! fail_forward {
-                                                                                               ($msg: expr, $err_code: expr, $err_data: expr) => {
+                                                                                               ($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr) => {
                                                                                                        {
                                                                                                                log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
                                                                                                                let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
-                                                                                                                       short_channel_id: short_chan_id,
+                                                                                                                       short_channel_id: prev_short_channel_id,
                                                                                                                        outpoint: prev_funding_outpoint,
                                                                                                                        htlc_id: prev_htlc_id,
                                                                                                                        incoming_packet_shared_secret: incoming_shared_secret,
+                                                                                                                       phantom_shared_secret: $phantom_ss,
                                                                                                                });
                                                                                                                failed_forwards.push((htlc_source, payment_hash,
-                                                                                                                               HTLCFailReason::Reason { failure_code: $err_code, data: $err_data }
+                                                                                                                       HTLCFailReason::Reason { failure_code: $err_code, data: $err_data }
                                                                                                                ));
                                                                                                                continue;
                                                                                                        }
                                                                                        if let PendingHTLCRouting::Forward { onion_packet, .. } = routing {
                                                                                                let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
                                                                                                if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
-                                                                                                       let shared_secret = {
+                                                                                                       let phantom_shared_secret = {
                                                                                                                let mut arr = [0; 32];
                                                                                                                arr.copy_from_slice(&SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap())[..]);
                                                                                                                arr
                                                                                                        };
-                                                                                                       let next_hop = match onion_utils::decode_next_hop(shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
+                                                                                                       let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
                                                                                                                Ok(res) => res,
                                                                                                                Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
-                                                                                                                       fail_forward!(err_msg, err_code, Vec::new());
+                                                                                                                       let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).into_inner();
+                                                                                                                       // In this scenario, the phantom would have sent us an
+                                                                                                                       // `update_fail_malformed_htlc`, meaning here we encrypt the error as
+                                                                                                                       // if it came from us (the second-to-last hop) but contains the sha256
+                                                                                                                       // of the onion.
+                                                                                                                       fail_forward!(err_msg, err_code, sha256_of_onion.to_vec(), None);
                                                                                                                },
                                                                                                                Err(onion_utils::OnionDecodeErr::Relay { err_msg, err_code }) => {
-                                                                                                                       fail_forward!(err_msg, err_code, Vec::new());
+                                                                                                                       fail_forward!(err_msg, err_code, Vec::new(), Some(phantom_shared_secret));
                                                                                                                },
                                                                                                        };
                                                                                                        match next_hop {
                                                                                                                onion_utils::Hop::Receive(hop_data) => {
-                                                                                                                       match self.construct_recv_pending_htlc_info(hop_data, shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value) {
+                                                                                                                       match self.construct_recv_pending_htlc_info(hop_data, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value, Some(phantom_shared_secret)) {
                                                                                                                                Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, vec![(info, prev_htlc_id)])),
-                                                                                                                               Err(ReceiveError { err_code, err_data, msg }) => fail_forward!(msg, err_code, err_data)
+                                                                                                                               Err(ReceiveError { err_code, err_data, msg }) => fail_forward!(msg, err_code, err_data, Some(phantom_shared_secret))
                                                                                                                        }
                                                                                                                },
                                                                                                                _ => panic!(),
                                                                                                        }
                                                                                                } else {
-                                                                                                       fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new());
+                                                                                                       fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);
                                                                                                }
                                                                                        } else {
-                                                                                               fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new());
+                                                                                               fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);
                                                                                        }
                                                                                },
                                                                        HTLCForwardInfo::FailHTLC { .. } => {
                                                                                // the channel is now on chain and our counterparty is
                                                                                // trying to broadcast the HTLC-Timeout, but that's their
                                                                                // problem, not ours.
-                                                                               //
-                                                                               // `fail_htlc_backwards_internal` is never called for
-                                                                               // phantom payments, so this is unreachable for them.
                                                                        }
                                                                }
                                                        }
                                                                                outpoint: prev_funding_outpoint,
                                                                                htlc_id: prev_htlc_id,
                                                                                incoming_packet_shared_secret: incoming_shared_secret,
+                                                                               // Phantom payments are only PendingHTLCRouting::Receive.
+                                                                               phantom_shared_secret: None,
                                                                        });
                                                                        match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet, &self.logger) {
                                                                                Err(e) => {
                                                        HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
                                                                        routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
                                                                        prev_funding_outpoint } => {
-                                                               let (cltv_expiry, onion_payload) = match routing {
-                                                                       PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry } =>
-                                                                               (incoming_cltv_expiry, OnionPayload::Invoice(payment_data)),
+                                                               let (cltv_expiry, onion_payload, phantom_shared_secret) = match routing {
+                                                                       PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry, phantom_shared_secret } =>
+                                                                               (incoming_cltv_expiry, OnionPayload::Invoice(payment_data), phantom_shared_secret),
                                                                        PendingHTLCRouting::ReceiveKeysend { payment_preimage, incoming_cltv_expiry } =>
-                                                                               (incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage)),
+                                                                               (incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage), None),
                                                                        _ => {
                                                                                panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
                                                                        }
                                                                                outpoint: prev_funding_outpoint,
                                                                                htlc_id: prev_htlc_id,
                                                                                incoming_packet_shared_secret: incoming_shared_secret,
+                                                                               phantom_shared_secret,
                                                                        },
                                                                        value: amt_to_forward,
                                                                        cltv_expiry,
                                                                                                outpoint: prev_funding_outpoint,
                                                                                                htlc_id: $htlc.prev_hop.htlc_id,
                                                                                                incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
+                                                                                               phantom_shared_secret,
                                                                                        }), payment_hash,
                                                                                        HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data }
                                                                                ));
  
                                                                macro_rules! check_total_value {
                                                                        ($payment_data_total_msat: expr, $payment_secret: expr, $payment_preimage: expr) => {{
 -                                                                              let mut total_value = 0;
                                                                                let mut payment_received_generated = false;
                                                                                let htlcs = channel_state.claimable_htlcs.entry(payment_hash)
                                                                                        .or_insert(Vec::new());
                                                                                                continue
                                                                                        }
                                                                                }
 -                                                                              htlcs.push(claimable_htlc);
 +                                                                              let mut total_value = claimable_htlc.value;
                                                                                for htlc in htlcs.iter() {
                                                                                        total_value += htlc.value;
                                                                                        match &htlc.onion_payload {
                                                                                if total_value >= msgs::MAX_VALUE_MSAT || total_value > $payment_data_total_msat {
                                                                                        log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the total value {} ran over expected value {} (or HTLCs were inconsistent)",
                                                                                                log_bytes!(payment_hash.0), total_value, $payment_data_total_msat);
 -                                                                                      for htlc in htlcs.iter() {
 -                                                                                              fail_htlc!(htlc);
 -                                                                                      }
 +                                                                                      fail_htlc!(claimable_htlc);
                                                                                } else if total_value == $payment_data_total_msat {
 +                                                                                      htlcs.push(claimable_htlc);
                                                                                        new_events.push(events::Event::PaymentReceived {
                                                                                                payment_hash,
                                                                                                purpose: events::PaymentPurpose::InvoicePayment {
                                                                                        // Nothing to do - we haven't reached the total
                                                                                        // payment value yet, wait until we receive more
                                                                                        // MPP parts.
 +                                                                                      htlcs.push(claimable_htlc);
                                                                                }
                                                                                payment_received_generated
                                                                        }}
                                pending_events.push(path_failure);
                                if let Some(ev) = full_failure_ev { pending_events.push(ev); }
                        },
-                       HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret, .. }) => {
+                       HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret, phantom_shared_secret, .. }) => {
                                let err_packet = match onion_error {
                                        HTLCFailReason::Reason { failure_code, data } => {
                                                log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
-                                               let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
-                                               onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
+                                               if let Some(phantom_ss) = phantom_shared_secret {
+                                                       let phantom_packet = onion_utils::build_failure_packet(&phantom_ss, failure_code, &data[..]).encode();
+                                                       let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(&phantom_ss, &phantom_packet);
+                                                       onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
+                                               } else {
+                                                       let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
+                                                       onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
+                                               }
                                        },
                                        HTLCFailReason::LightningError { err } => {
                                                log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards with pre-built LightningError", log_bytes!(payment_hash.0));
                                                                        onion_utils::build_first_hop_failure_packet(incoming_shared_secret, error_code, &{
                                                                                let mut res = Vec::with_capacity(8 + 128);
                                                                                // TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791
-                                                                               res.extend_from_slice(&byte_utils::be16_to_array(0));
+                                                                               if error_code == 0x1000 | 20 {
+                                                                                       res.extend_from_slice(&byte_utils::be16_to_array(0));
+                                                                               }
                                                                                res.extend_from_slice(&upd.encode_with_len()[..]);
                                                                                res
                                                                        }[..])
        /// In chanmon_consistency_target, we'd like to be able to restore monitor updating without
        /// handling all pending events (i.e. not PendingHTLCsForwardable). Thus, we expose monitor
        /// update events as a separate process method here.
 -      #[cfg(feature = "fuzztarget")]
 +      #[cfg(fuzzing)]
        pub fn process_monitor_events(&self) {
                self.process_pending_monitor_events();
        }
                }
        }
  
 -      #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
 +      #[cfg(any(test, fuzzing, feature = "_test_utils"))]
        pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
                let events = core::cell::RefCell::new(Vec::new());
                let event_handler = |event: &events::Event| events.borrow_mut().push(event.clone());
@@@ -5978,6 -5997,7 +5996,7 @@@ impl_writeable_tlv_based_enum!(PendingH
        },
        (1, Receive) => {
                (0, payment_data, required),
+               (1, phantom_shared_secret, option),
                (2, incoming_cltv_expiry, required),
        },
        (2, ReceiveKeysend) => {
@@@ -6069,6 -6089,7 +6088,7 @@@ impl_writeable_tlv_based_enum!(PendingH
  
  impl_writeable_tlv_based!(HTLCPreviousHopData, {
        (0, short_channel_id, required),
+       (1, phantom_shared_secret, option),
        (2, outpoint, required),
        (4, htlc_id, required),
        (6, incoming_packet_shared_secret, required)
diff --combined lightning/src/ln/msgs.rs
index ffca10dbb6de009ed2b5bfa53f1a88f2324ce453,131c4fb54d2c51a4698a9e390b9456d08b07a2e4..7295c40d5442dd87639d08bbe6a6a3b2c1d5a873
@@@ -204,12 -204,6 +204,12 @@@ pub struct AcceptChannel 
        pub first_per_commitment_point: PublicKey,
        /// Optionally, a request to pre-set the to-sender output's scriptPubkey for when we collaboratively close
        pub shutdown_scriptpubkey: OptionalField<Script>,
 +      /// The channel type that this channel will represent. If none is set, we derive the channel
 +      /// type from the intersection of our feature bits with our counterparty's feature bits from
 +      /// the Init message.
 +      ///
 +      /// This is required to match the equivalent field in [`OpenChannel::channel_type`].
 +      pub channel_type: Option<ChannelTypeFeatures>,
  }
  
  /// A funding_created message to be sent or received from a peer
@@@ -943,9 -937,9 +943,9 @@@ mod fuzzy_internal_msgs 
                pub(crate) pad: Vec<u8>,
        }
  }
 -#[cfg(feature = "fuzztarget")]
 +#[cfg(fuzzing)]
  pub use self::fuzzy_internal_msgs::*;
 -#[cfg(not(feature = "fuzztarget"))]
 +#[cfg(not(fuzzing))]
  pub(crate) use self::fuzzy_internal_msgs::*;
  
  #[derive(Clone)]
@@@ -1070,9 -1064,7 +1070,9 @@@ impl_writeable_msg!(AcceptChannel, 
        htlc_basepoint,
        first_per_commitment_point,
        shutdown_scriptpubkey
 -}, {});
 +}, {
 +      (1, channel_type, option),
 +});
  
  impl_writeable_msg!(AnnouncementSignatures, {
        channel_id,
@@@ -1299,10 -1291,6 +1299,6 @@@ impl Readable for FinalOnionHopData 
  
  impl Writeable for OnionHopData {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
-               // Note that this should never be reachable if Rust-Lightning generated the message, as we
-               // check values are sane long before we get here, though its possible in the future
-               // user-generated messages may hit this.
-               if self.amt_to_forward > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
                match self.format {
                        OnionHopDataFormat::Legacy { short_channel_id } => {
                                0u8.write(w)?;
                                });
                        },
                        OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => {
-                               if let Some(final_data) = payment_data {
-                                       if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
-                               }
                                encode_varint_length_prefixed_tlv!(w, {
                                        (2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required),
                                        (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required),
@@@ -2199,8 -2184,7 +2192,8 @@@ mod tests 
                        delayed_payment_basepoint: pubkey_4,
                        htlc_basepoint: pubkey_5,
                        first_per_commitment_point: pubkey_6,
 -                      shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
 +                      shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent },
 +                      channel_type: None,
                };
                let encoded_value = accept_channel.encode();
                let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();