psbt: fix finalizing large P2SH and bare multisig inputs
What changed, and why it matters
This commit fixes two bugs in how the library finalizes old-style multi-signature Bitcoin transactions. First, large multisig setups (like 5-of-5 or 4-of-7) were silently left unfinished because the buffer used was too small. Second, for bare multisig transactions, the final script incorrectly included the whole redeem script as if it were another signature, making the spend invalid. The patch allocates a correctly sized buffer and only appends the redeem script for P2SH inputs.
Review and merge the patch, then run the new regression tests. Users relying on legacy multisig PSBT finalization should upgrade, especially those using large P2SH multisig or bare multisig configurations.
Security signals we found
Buffer size mismatch causing silent failure to finalize large multisig inputs
Incorrect scriptSig construction for bare multisig producing invalid spends
Heap allocation introduced to replace undersized stack buffer
New regression tests for large P2SH and bare multisig finalization
Evidence from the diff
In src/psbt.c, finalize_multisig previously built legacy multisig scriptSigs in a fixed WALLY_SCRIPTSIG_MAX_LEN (520 byte) stack buffer. That constant is the limit for a single push, not for a scriptSig, so P2SH inputs needing more (e.g. 15-of-15, 5-of-5, 4-of-7, 1-of-13) were silently left unfinalized. The patch switches to a heap allocation sized for OP_0 + all DER signatures + the redeem script push. It also fixes bare multisig: previously wally_scriptsig_multisig_from_bytes appended the redeem script push unconditionally, so CHECKMULTISIG treated it as an extra signature and failed; now for bare multisig the script length is reduced by push_len so only signatures are pushed. Tests are added for 15-of-15 P2SH and 2-of-3 bare multisig.
Changed components
src/psbt.c finalize_multisig functionsrc/ctest/test_psbt.c regression testslibwally-core PSBT finalization for legacy P2SH and bare multisig inputsInspect captured patch +136 / −8
### src/ctest/test_psbt.c
@@ -3,6 +3,7 @@
#include <wally_address.h>
#include <wally_crypto.h>
#include <wally_psbt.h>
+#include <wally_script.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
@@ -145,6 +146,122 @@ static void check_signer_modifiable_flags(void)
wally_bzero(priv_key, sizeof(priv_key));
}
+/* Finalizing a legacy multisig input gives OP_0 <sig>..., followed by the
+ * redeem script push for P2SH only */
+static void check_finalize_legacy_multisig(void)
+{
+ static const struct {
+ uint32_t threshold;
+ size_t n_keys;
+ bool is_p2sh;
+ } cases[] = {
+ { 15, 15, true }, /* The largest P2SH multisig scriptSig */
+ { 2, 3, false }, /* Bare multisig */
+ };
+ unsigned char priv_keys[15][EC_PRIVATE_KEY_LEN];
+ unsigned char pub_keys[15 * EC_PUBLIC_KEY_LEN];
+ unsigned char multisig[3 + 15 * (EC_PUBLIC_KEY_LEN + 1)];
+ unsigned char p2sh[WALLY_SCRIPTPUBKEY_P2SH_LEN], txhash[WALLY_TXHASH_LEN];
+ unsigned char expected[1650]; /* MAX_STANDARD_SCRIPTSIG_SIZE */
+ const unsigned char fingerprint[4] = { 0 };
+ size_t i, j;
+
+ for (i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
+ const uint32_t threshold = cases[i].threshold;
+ const size_t n_keys = cases[i].n_keys;
+ const unsigned char *spk = multisig;
+ size_t multisig_len, spk_len, expected_len = 1, written, is_finalized;
+ struct wally_tx *utxo = NULL, *tx = NULL, *final_tx = NULL;
+ struct wally_psbt *psbt = NULL;
+
+ for (j = 0; j < n_keys; ++j) {
+ memset(priv_keys[j], (int)j + 1, EC_PRIVATE_KEY_LEN);
+ if (wally_ec_public_key_from_private_key(priv_keys[j], EC_PRIVATE_KEY_LEN,
+ pub_keys + j * EC_PUBLIC_KEY_LEN,
+ EC_PUBLIC_KEY_LEN) != WALLY_OK)
+ fail("Failed to create public key");
+ }
+ if (wally_scriptpubkey_multisig_from_bytes(pub_keys, n_keys * EC_PUBLIC_KEY_LEN,
+ threshold, 0, multisig, sizeof(multisig),
+ &multisig_len) != WALLY_OK)
+ fail("Failed to create %u-of-%zu multisig script", threshold, n_keys);
+ spk_len = multisig_len;
+ if (cases[i].is_p2sh) {
+ if (wally_scriptpubkey_p2sh_from_bytes(multisig, multisig_len, WALLY_SCRIPT_HASH160,
+ p2sh, sizeof(p2sh), &spk_len) != WALLY_OK)
+ fail("Failed to create p2sh script");
+ spk = p2sh;
+ }
+
+ memset(txhash, 0x11, sizeof(txhash));
+ if (wally_tx_init_alloc(WALLY_TX_VERSION_2, 0, 1, 1, &utxo) != WALLY_OK ||
+ wally_tx_add_raw_input(utxo, txhash, sizeof(txhash), 0, 0xffffffff,
+ NULL, 0, NULL, 0) != WALLY_OK ||
+ wally_tx_add_raw_output(utxo, 100000, spk, spk_len, 0) != WALLY_OK ||
+ wally_tx_get_txid(utxo, txhash, sizeof(txhash)) != WALLY_OK ||
+ wally_tx_init_alloc(WALLY_TX_VERSION_2, 0, 1, 1, &tx) != WALLY_OK ||
+ wally_tx_add_raw_input(tx, txhash, sizeof(txhash), 0, 0xffffffff,
+ NULL, 0, NULL, 0) != WALLY_OK ||
+ wally_tx_add_raw_output(tx, 90000, spk, spk_len, 0) != WALLY_OK ||
+ wally_psbt_from_tx(tx, WALLY_PSBT_VERSION_0, 0, &psbt) != WALLY_OK ||
+ wally_psbt_input_set_utxo(&psbt->inputs[0], utxo) != WALLY_OK ||
+ (cases[i].is_p2sh &&
+ wally_psbt_input_set_redeem_script(&psbt->inputs[0], multisig,
+ multisig_len) != WALLY_OK))
+ fail("Failed to create %u-of-%zu psbt", threshold, n_keys);
+ for (j = 0; j < n_keys; ++j)
+ if (wally_psbt_add_input_keypath(psbt, 0, pub_keys + j * EC_PUBLIC_KEY_LEN,
+ EC_PUBLIC_KEY_LEN, fingerprint,
+ sizeof(fingerprint), NULL, 0) != WALLY_OK)
+ fail("Failed to add keypath");
+ for (j = 0; j < threshold; ++j)
+ if (wally_psbt_sign(psbt, priv_keys[j], EC_PRIVATE_KEY_LEN, 0) != WALLY_OK)
+ fail("Failed to sign %u-of-%zu psbt", threshold, n_keys);
+ if (psbt->inputs[0].signatures.num_items != threshold)
+ fail("%u-of-%zu psbt has %zu signatures", threshold, n_keys,
+ psbt->inputs[0].signatures.num_items);
+
+ /* The signatures in key order, then the redeem script for P2SH */
+ expected[0] = OP_0;
+ for (j = 0; j <= threshold; ++j) {
+ const unsigned char *push = multisig;
+ size_t push_len = multisig_len;
+ if (j < threshold) {
+ const struct wally_map_item *sig;
+ sig = wally_map_get(&psbt->inputs[0].signatures,
+ pub_keys + j * EC_PUBLIC_KEY_LEN, EC_PUBLIC_KEY_LEN);
+ if (!sig)
+ fail("Missing signature %zu", j);
+ push = sig->value;
+ push_len = sig->value_len;
+ } else if (!cases[i].is_p2sh)
+ break;
+ if (wally_script_push_from_bytes(push, push_len, 0, expected + expected_len,
+ sizeof(expected) - expected_len,
+ &written) != WALLY_OK ||
+ written > sizeof(expected) - expected_len)
+ fail("Failed to build the expected scriptSig");
+ expected_len += written;
+ }
+
+ if (wally_psbt_finalize(psbt, 0) != WALLY_OK ||
+ wally_psbt_is_finalized(psbt, &is_finalized) != WALLY_OK || !is_finalized)
+ fail("%u-of-%zu %s psbt was not finalized", threshold, n_keys,
+ cases[i].is_p2sh ? "p2sh" : "bare");
+ if (wally_psbt_extract(psbt, 0, &final_tx) != WALLY_OK ||
+ final_tx->inputs[0].script_len != expected_len ||
+ memcmp(final_tx->inputs[0].script, expected, expected_len))
+ fail("%u-of-%zu %s psbt has the wrong scriptSig", threshold, n_keys,
+ cases[i].is_p2sh ? "p2sh" : "bare");
+
+ wally_psbt_free(psbt);
+ wally_tx_free(final_tx);
+ wally_tx_free(tx);
+ wally_tx_free(utxo);
+ }
+ wally_bzero(priv_keys, sizeof(priv_keys));
+}
+
int main(void)
{
size_t i;
@@ -296,6 +413,7 @@ int main(void)
}
check_signer_modifiable_flags();
+ check_finalize_legacy_multisig();
wally_cleanup(0);
return 0;
### src/psbt.c
@@ -5155,15 +5155,25 @@ static bool finalize_multisig(struct wally_psbt_input *input,
if (is_p2sh && !finalize_p2sh_wrapped(input))
goto fail;
} else {
- unsigned char script[WALLY_SCRIPTSIG_MAX_LEN];
+ /* OP_0 <sig>... <script>. This can be larger than
+ * WALLY_SCRIPTSIG_MAX_LEN, which limits a single push */
+ const size_t push_len = script_get_push_size(out_script_len);
+ const size_t max_len = 1 + n_found * (EC_SIGNATURE_DER_MAX_LEN + 2) + push_len;
+ unsigned char *script = wally_malloc(max_len);
size_t script_len;
-
- if (wally_scriptsig_multisig_from_bytes(out_script, out_script_len,
- sigs, n_found * EC_SIGNATURE_LEN,
- sighashes, n_found, 0,
- script, sizeof(script), &script_len) != WALLY_OK ||
- script_len > sizeof(script) ||
- wally_psbt_input_set_final_scriptsig(input, script, script_len) != WALLY_OK)
+ bool ok;
+
+ ok = script &&
+ wally_scriptsig_multisig_from_bytes(out_script, out_script_len,
+ sigs, n_found * EC_SIGNATURE_LEN,
+ sighashes, n_found, 0,
+ script, max_len, &script_len) == WALLY_OK &&
+ script_len <= max_len;
+ if (ok && !is_p2sh)
+ script_len -= push_len; /* Bare multisig: don't push the script */
+ ok = ok && wally_psbt_input_set_final_scriptsig(input, script, script_len) == WALLY_OK;
+ clear_and_free(script, max_len);
+ if (!ok)
goto fail;
}
ret = true;Why this scored 60/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.