From 39b34a35c88a7ade9128978ae0955f24268addf6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 21 Aug 2019 13:22:42 +0930 Subject: [PATCH] bitcoin/tx.c: don't free witness implicitly. This causes a crash in mkfunding, which didn't expect it: $ devtools/mkfunding 16835ac8c154b616baac524163f41fb0c4f82c7b972ad35d4d6f18d854f6856b 1 0.01btc 253 76edf0c303b9e692da9cb491abedef46ca5b81d32f102eb4648461b239cb0f99 0000000000000000000000000000000000000000000000000000000000000010 0000000000000000000000000000000000000000000000000000000000000020 # funding sig: 798d96d5a057b5b7797988a855217f41af05ece3ba8278366e2f69763c72e78565d5dd7eeddc0766ddf65557c92b9c52c301f23f94d2cf681860d32153e6ae1e # funding witnesses: [ Aborted (core dumped) Signed-off-by: Rusty Russell --- bitcoin/tx.c | 3 ++- bitcoin/tx.h | 2 +- channeld/test/run-commit_tx.c | 3 ++- common/htlc_tx.c | 4 ++-- devtools/mkcommit.c | 2 +- hsmd/hsmd.c | 2 +- lightningd/peer_control.c | 2 +- onchaind/onchaind.c | 6 +++--- 8 files changed, 13 insertions(+), 11 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index cfec63c8c..72b5e7672 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -117,7 +117,8 @@ void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum, wally_tx_set_input_witness(tx->wtx, innum, stack); if (stack) wally_tx_witness_stack_free(stack); - tal_free(witness); + if (taken(witness)) + tal_free(witness); } void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script) diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 8cdeb6f42..358a11520 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -116,7 +116,7 @@ struct amount_sat bitcoin_tx_output_get_amount(const struct bitcoin_tx *tx, * itself, we need a way to attach a witness to an existing input. */ void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum, - u8 **witness); + u8 **witness TAKES); /** * Set the input script on the given input. diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index d63919237..f220d8d34 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -350,7 +350,7 @@ static void report(struct bitcoin_tx *tx, witness = bitcoin_witness_2of2(tx, &localsig, &remotesig, local_funding_pubkey, remote_funding_pubkey); - bitcoin_tx_input_set_witness(tx, 0, witness); + bitcoin_tx_input_set_witness(tx, 0, take(witness)); txhex = tal_hex(tmpctx, linearize_tx(tx, tx)); printf("output commit_tx: %s\n", txhex); @@ -993,6 +993,7 @@ int main(void) /* No memory leaks please */ secp256k1_context_destroy(secp256k1_ctx); + take_cleanup(); tal_free(tmpctx); /* FIXME: Do BOLT comparison! */ diff --git a/common/htlc_tx.c b/common/htlc_tx.c index 819b1e392..d41044d07 100644 --- a/common/htlc_tx.c +++ b/common/htlc_tx.c @@ -109,7 +109,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success, witness = bitcoin_witness_htlc_success_tx(htlc_success, localhtlcsig, remotehtlcsig, payment_preimage, wscript); - bitcoin_tx_input_set_witness(htlc_success, 0, witness); + bitcoin_tx_input_set_witness(htlc_success, 0, take(witness)); tal_free(wscript); } @@ -149,7 +149,7 @@ void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout, witness = bitcoin_witness_htlc_timeout_tx(htlc_timeout, localhtlcsig, remotehtlcsig, wscript); - bitcoin_tx_input_set_witness(htlc_timeout, 0, witness); + bitcoin_tx_input_set_witness(htlc_timeout, 0, take(witness)); tal_free(wscript); } diff --git a/devtools/mkcommit.c b/devtools/mkcommit.c index 540c9ee06..8d2b27790 100644 --- a/devtools/mkcommit.c +++ b/devtools/mkcommit.c @@ -396,7 +396,7 @@ int main(int argc, char *argv[]) witness = bitcoin_witness_2of2(NULL, &local_sig, &remote_sig, &funding_localkey, &funding_remotekey); - bitcoin_tx_input_set_witness(local_txs[0], 0, witness); + bitcoin_tx_input_set_witness(local_txs[0], 0, take(witness)); printf("# signed local commitment: %s\n", tal_hex(NULL, linearize_tx(NULL, local_txs[0]))); diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index 0e4efcefb..53f06b41a 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -1457,7 +1457,7 @@ static void sign_all_inputs(struct bitcoin_tx *tx, struct utxo **utxos) /* The witness is [sig] [key] */ bitcoin_tx_input_set_witness( - tx, i, bitcoin_witness_p2wpkh(tx, &sig, &inkey)); + tx, i, take(bitcoin_witness_p2wpkh(tx, &sig, &inkey))); } } diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index d8dbead86..172cd0b25 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -224,7 +224,7 @@ static void sign_last_tx(struct channel *channel) &sig, &channel->channel_info.remote_fundingkey, &channel->local_funding_pubkey); - bitcoin_tx_input_set_witness(channel->last_tx, 0, witness); + bitcoin_tx_input_set_witness(channel->last_tx, 0, take(witness)); } static void remove_sig(struct bitcoin_tx *signed_tx) diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index f3517e2f9..3fb0733ce 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -368,7 +368,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, witness = bitcoin_witness_sig_and_element(tx, &sig, elem, elemsize, wscript); - bitcoin_tx_input_set_witness(tx, 0, witness); + bitcoin_tx_input_set_witness(tx, 0, take(witness)); return tx; } @@ -1277,7 +1277,7 @@ static void handle_preimage(const struct chainparams *chainparams, witness = bitcoin_witness_htlc_success_tx( tx, &sig, outs[i]->remote_htlc_sig, preimage, outs[i]->wscript); - bitcoin_tx_input_set_witness(tx, 0, witness); + bitcoin_tx_input_set_witness(tx, 0, take(witness)); propose_resolution(outs[i], tx, 0, OUR_HTLC_SUCCESS_TX); } else { enum tx_type tx_type = THEIR_HTLC_FULFILL_TO_US; @@ -1530,7 +1530,7 @@ static size_t resolve_our_htlc_ourcommit(const struct chainparams *chainparams, out->remote_htlc_sig, htlc_scripts[matches[i]]); - bitcoin_tx_input_set_witness(tx, 0, witness); + bitcoin_tx_input_set_witness(tx, 0, take(witness)); /* Steals tx onto out */ propose_resolution_at_block(out, tx, htlcs[matches[i]].cltv_expiry,