@@ -37,7 +37,7 @@ use util::config::UserConfig;
37
37
38
38
use bitcoin::hash_types::BlockHash;
39
39
use bitcoin::blockdata::block::{Block, BlockHeader};
40
- use bitcoin::blockdata::script::Builder;
40
+ use bitcoin::blockdata::script::{ Builder, Script} ;
41
41
use bitcoin::blockdata::opcodes;
42
42
use bitcoin::blockdata::constants::genesis_block;
43
43
use bitcoin::network::constants::Network;
@@ -9424,6 +9424,10 @@ fn test_invalid_funding_tx() {
9424
9424
// funding transactions from their counterparties, leading to a multi-implementation critical
9425
9425
// security vulnerability (though we always sanitized properly, we've previously had
9426
9426
// un-released crashes in the sanitization process).
9427
+ //
9428
+ // Further, if the funding transaction is consensus-valid, confirms, and is later spent, we'd
9429
+ // previously have crashed in `ChannelMonitor` even though we closed the channel as bogus and
9430
+ // gave up on it. We test this here by generating such a transaction.
9427
9431
let chanmon_cfgs = create_chanmon_cfgs(2);
9428
9432
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9429
9433
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -9434,9 +9438,19 @@ fn test_invalid_funding_tx() {
9434
9438
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9435
9439
9436
9440
let (temporary_channel_id, mut tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
9441
+
9442
+ // Create a witness program which can be spent by a 4-empty-stack-elements witness and which is
9443
+ // 136 bytes long. This matches our "accepted HTLC preimage spend" matching, previously causing
9444
+ // a panic as we'd try to extract a 32 byte preimage from a witness element without checking
9445
+ // its length.
9446
+ let mut wit_program: Vec<u8> = channelmonitor::deliberately_bogus_accepted_htlc_witness_program();
9447
+ assert!(chan_utils::HTLCType::scriptlen_to_htlctype(wit_program.len()).unwrap() ==
9448
+ chan_utils::HTLCType::AcceptedHTLC);
9449
+
9450
+ let wit_program_script: Script = wit_program.clone().into();
9437
9451
for output in tx.output.iter_mut() {
9438
9452
// Make the confirmed funding transaction have a bogus script_pubkey
9439
- output.script_pubkey = bitcoin:: Script::new( );
9453
+ output.script_pubkey = Script::new_v0_p2wsh(&wit_program_script.wscript_hash() );
9440
9454
}
9441
9455
9442
9456
nodes[0].node.funding_transaction_generated_unchecked(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone(), 0).unwrap();
@@ -9466,6 +9480,28 @@ fn test_invalid_funding_tx() {
9466
9480
} else { panic!(); }
9467
9481
} else { panic!(); }
9468
9482
assert_eq!(nodes[1].node.list_channels().len(), 0);
9483
+
9484
+ // Now confirm a spend of the (bogus) funding transaction. As long as the witness is 5 elements
9485
+ // long the ChannelMonitor will try to read 32 bytes from the second-to-last element, panicing
9486
+ // as its not 32 bytes long.
9487
+ let mut spend_tx = Transaction {
9488
+ version: 2i32, lock_time: 0,
9489
+ input: tx.output.iter().enumerate().map(|(idx, _)| TxIn {
9490
+ previous_output: BitcoinOutPoint {
9491
+ txid: tx.txid(),
9492
+ vout: idx as u32,
9493
+ },
9494
+ script_sig: Script::new(),
9495
+ sequence: 0xfffffffd,
9496
+ witness: Witness::from_vec(channelmonitor::deliberately_bogus_accepted_htlc_witness())
9497
+ }).collect(),
9498
+ output: vec![TxOut {
9499
+ value: 1000,
9500
+ script_pubkey: Script::new(),
9501
+ }]
9502
+ };
9503
+ check_spends!(spend_tx, tx);
9504
+ mine_transaction(&nodes[1], &spend_tx);
9469
9505
}
9470
9506
9471
9507
fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
0 commit comments