Sanitize outgoing HTLC cltv_value
authorAntoine Riard <ariard@student.42.fr>
Fri, 10 Apr 2020 02:04:30 +0000 (22:04 -0400)
committerMatt Corallo <git@bluematt.me>
Thu, 16 Apr 2020 01:31:09 +0000 (21:31 -0400)
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs

index ffa65209710a6f6b3b51e14cf7f36e9a2ceb7bad..3da9999d3eeae550ac3f1b3172d16372c5a8d8c2 100644 (file)
@@ -3538,8 +3538,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        return Err(ChannelError::Ignore("Cannot send value that would put us over their reserve value"));
                }
 
-               //TODO: Check cltv_expiry? Do this in channel manager?
-
                // Now update local state:
                if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
                        self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
index 2ea6ff2b0d7c5020ca72a04dce0ad5de508846e8..f12a260ce7bd5ff0ab17b6b34f1f9805b7cbab4d 100644 (file)
@@ -1172,6 +1172,13 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
                                        if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
                                                break Some(("CLTV expiry is too far in the future", 21, None));
                                        }
+                                       // We should be safe against unintentional channel-closure by our onchain logic due to this later
+                                       // offering a LATENCY_GRACE_PERIOD_BLOCKS. But to make our policy coherent with reception, apply
+                                       // same delay.
+                                       if (*outgoing_cltv_value) as u64 <= (cur_height + CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
+                                               break Some(("Outgoing CLTV value is too close", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
+                                       }
+
                                        break None;
                                }
                                {