@@ -37,7 +37,7 @@ use util::config::UserConfig;
3737
3838use bitcoin::hash_types::BlockHash;
3939use bitcoin::blockdata::block::{Block, BlockHeader};
40- use bitcoin::blockdata::script::Builder;
40+ use bitcoin::blockdata::script::{ Builder, Script} ;
4141use bitcoin::blockdata::opcodes;
4242use bitcoin::blockdata::constants::genesis_block;
4343use bitcoin::network::constants::Network;
@@ -9424,6 +9424,10 @@ fn test_invalid_funding_tx() {
94249424 // funding transactions from their counterparties, leading to a multi-implementation critical
94259425 // security vulnerability (though we always sanitized properly, we've previously had
94269426 // 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.
94279431 let chanmon_cfgs = create_chanmon_cfgs(2);
94289432 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
94299433 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -9434,9 +9438,19 @@ fn test_invalid_funding_tx() {
94349438 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()));
94359439
94369440 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();
94379451 for output in tx.output.iter_mut() {
94389452 // 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() );
94409454 }
94419455
94429456 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() {
94669480 } else { panic!(); }
94679481 } else { panic!(); }
94689482 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);
94699505}
94709506
94719507fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
0 commit comments