Skip to content

Commit 88b7dcd

Browse files
authored
Merge pull request #445 from TheBlueMatt/2020-01-fuzz-enforcer-fix
Fix EnforcingChannelKeys panic when our counterparty burns their $.
2 parents 6f5a48b + 1443509 commit 88b7dcd

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
240240
secp_ctx: Secp256k1<secp256k1::All>,
241241
channel_value_satoshis: u64,
242242

243+
#[cfg(not(test))]
243244
local_keys: ChanSigner,
245+
#[cfg(test)]
246+
pub(super) local_keys: ChanSigner,
244247
shutdown_pubkey: PublicKey,
245248

246249
// Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
@@ -1995,6 +1998,17 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
19951998
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
19961999
.map_err(|e| ChannelError::Close(e.0))?;
19972000

2001+
if self.channel_state & ChannelState::AwaitingRemoteRevoke as u32 == 0 {
2002+
// Our counterparty seems to have burned their coins to us (by revoking a state when we
2003+
// haven't given them a new commitment transaction to broadcast). We should probably
2004+
// take advantage of this by updating our channel monitor, sending them an error, and
2005+
// waiting for them to broadcast their latest (now-revoked claim). But, that would be a
2006+
// lot of work, and there's some chance this is all a misunderstanding anyway.
2007+
// We have to do *something*, though, since our signer may get mad at us for otherwise
2008+
// jumping a remote commitment number, so best to just force-close and move on.
2009+
return Err(ChannelError::Close("Received an unexpected revoke_and_ack"));
2010+
}
2011+
19982012
// Update state now that we've passed all the can-fail calls...
19992013
// (note that we may still fail to generate the new commitment_signed message, but that's
20002014
// OK, we step the channel here and *then* if the new generation fails we can fail the

lightning/src/ln/functional_tests.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
99
use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT};
1010
use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
1111
use ln::channel::{Channel, ChannelError};
12-
use ln::onion_utils;
12+
use ln::{chan_utils, onion_utils};
1313
use ln::router::{Route, RouteHop};
1414
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1515
use ln::msgs;
@@ -6972,6 +6972,32 @@ fn test_set_outpoints_partial_claiming() {
69726972
}
69736973
}
69746974

6975+
#[test]
6976+
fn test_counterparty_raa_skip_no_crash() {
6977+
// Previously, if our counterparty sent two RAAs in a row without us having provided a
6978+
// commitment transaction, we would have happily carried on and provided them the next
6979+
// commitment transaction based on one RAA forward. This would probably eventually have led to
6980+
// channel closure, but it would not have resulted in funds loss. Still, our
6981+
// EnforcingChannelKeys would have paniced as it doesn't like jumps into the future. Here, we
6982+
// check simply that the channel is closed in response to such an RAA, but don't check whether
6983+
// we decide to punish our counterparty for revoking their funds (as we don't currently
6984+
// implement that).
6985+
let node_cfgs = create_node_cfgs(2);
6986+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6987+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6988+
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::supported(), InitFeatures::supported()).2;
6989+
6990+
let commitment_seed = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&channel_id).unwrap().local_keys.commitment_seed().clone();
6991+
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
6992+
let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
6993+
&SecretKey::from_slice(&chan_utils::build_commitment_secret(&commitment_seed, INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
6994+
let per_commitment_secret = chan_utils::build_commitment_secret(&commitment_seed, INITIAL_COMMITMENT_NUMBER);
6995+
6996+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
6997+
&msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });
6998+
assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
6999+
}
7000+
69757001
#[test]
69767002
fn test_bump_txn_sanitize_tracking_maps() {
69777003
// Sanitizing pendning_claim_request and claimable_outpoints used to be buggy,

0 commit comments

Comments
 (0)