From de67bfa477d3e8ceff11e5b11bf0e329018429f7 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:19:13 +0900 Subject: [PATCH 1/3] psbt: Refuse v2 global tx under loose parsing wally_psbt_from_bytes() with WALLY_PSBT_PARSE_FLAG_LOOSE accepted a v2 PSBT carrying PSBT_GLOBAL_UNSIGNED_TX if the tx key came before the version key. psbt_set_global_tx() allocated the input and output arrays for the tx, then the v2 path overwrote those pointers with new arrays, leaking the old ones. Refuse such a PSBT instead of freeing the arrays. A v2 PSBT with a tx fails psbt_is_valid(), so it could not even be serialized, and the reverse key order is already refused by psbt_set_global_tx(). The signature-only PSBTs that loose parsing exists for never have a tx in v2. --- src/psbt.c | 4 +++- src/test/test_psbt.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/psbt.c b/src/psbt.c index a938ae2ad..1efba9724 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -2877,7 +2877,9 @@ int wally_psbt_from_bytes(const unsigned char *bytes, size_t len, } if (ret == WALLY_OK && (*output)->version == PSBT_2) { - if ((*output)->tx_version < 2) + if ((*output)->tx) + ret = WALLY_EINVAL; /* v2 cannot have a tx, even when parsing loosely */ + else if ((*output)->tx_version < 2) ret = WALLY_EINVAL; /* Tx version must be >= 2 */ else { struct wally_psbt tmp; diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 095244568..9bb9f6a09 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -41,6 +41,26 @@ def test_invalid(self): for case in JSON['invalid']: wally_psbt_free(self.parse_base64(case['psbt'], WALLY_EINVAL)) + def test_v2_global_tx_loose(self): + """A v2 PSBT cannot hold a global tx, even when parsing loosely""" + FLAG_LOOSE = 0x2 # WALLY_PSBT_PARSE_FLAG_LOOSE + version = '01fb0402000000' + tx = '0200000001' + '11' * 32 + '0000000000ffffffff01' + '00' * 8 + '015100000000' + global_tx = '0100' + '%02x' % (len(tx) // 2) + tx + + def parse(globals_hex): + buf, buf_len = make_cbuffer('70736274ff' + globals_hex + '00') + psbt = pointer(wally_psbt()) + ret = wally_psbt_from_bytes(buf, buf_len, FLAG_LOOSE, psbt) + if ret == WALLY_OK: + wally_psbt_free(psbt) + return ret + + self.assertEqual(parse(version), WALLY_OK) + # The tx is refused wherever it appears relative to the version + self.assertEqual(parse(global_tx + version), WALLY_EINVAL) + self.assertEqual(parse(version + global_tx), WALLY_EINVAL) + def test_valid(self): """Test deserializing and roundtripping valid PSBTs""" buf, buf_len = make_cbuffer('00' * 4096) From f950c9b6dfc09c4274d7a6589698e141476dc5b4 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:22:32 +0900 Subject: [PATCH 2/3] psbt: Refuse upgrading a v0 PSBT with no tx wally_psbt_set_version() upgrading a v0 PSBT to v2 read the version, locktime, inputs and outputs from psbt->tx without checking it exists. A v0 PSBT with no global tx is valid: wally_psbt_init_alloc() makes one, and loose parsing accepts one, so set_version() on either dereferenced NULL. Refuse the upgrade with WALLY_EINVAL, as psbt_build_tx() and the other v0 paths already do when there is no tx. --- src/psbt.c | 3 +++ src/test/test_psbt.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/psbt.c b/src/psbt.c index 1efba9724..b6c316437 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -4322,6 +4322,9 @@ int wally_psbt_set_version(struct wally_psbt *psbt, if (!psbt_is_valid(psbt) || flags || (version != PSBT_0 && version != PSBT_2)) return WALLY_EINVAL; + if (psbt->version == PSBT_0 && version == PSBT_2 && !psbt->tx) + return WALLY_EINVAL; /* Nothing to upgrade from */ + if (psbt->version == version) return WALLY_OK; /* No-op */ diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 9bb9f6a09..ad3e41433 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -61,6 +61,21 @@ def parse(globals_hex): self.assertEqual(parse(global_tx + version), WALLY_EINVAL) self.assertEqual(parse(version + global_tx), WALLY_EINVAL) + def test_set_version_without_tx(self): + """A v0 PSBT with no global tx cannot be upgraded to v2""" + FLAG_LOOSE = 0x2 # WALLY_PSBT_PARSE_FLAG_LOOSE + empty = pointer(wally_psbt()) + self.assertEqual(wally_psbt_init_alloc(0, 0, 0, 0, 0, empty), WALLY_OK) + buf, buf_len = make_cbuffer('70736274ff00') + loose = pointer(wally_psbt()) + self.assertEqual(wally_psbt_from_bytes(buf, buf_len, FLAG_LOOSE, loose), + WALLY_OK) + for psbt in [empty, loose]: + self.assertEqual(wally_psbt_set_version(psbt, 0, 0), WALLY_OK) + self.assertEqual(wally_psbt_set_version(psbt, 0, 2), WALLY_EINVAL) + self.assertEqual(wally_psbt_get_version(psbt), (WALLY_OK, 0)) + wally_psbt_free(psbt) + def test_valid(self): """Test deserializing and roundtripping valid PSBTs""" buf, buf_len = make_cbuffer('00' * 4096) From 3f023fd9fb46b3c8e8400dc5652e5c988c074fa7 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:12:10 +0900 Subject: [PATCH 3/3] psbt: Copy the PSET genesis blockhash wally_psbt_clone_alloc() and wally_psbt_combine() copied every global PSET field except the genesis blockhash, so a cloned PSET serialized without it and its taproot inputs could no longer be signed, as the Elements taproot signature hash requires the genesis blockhash. Copy it when the destination has none. A clone always takes it from the source, and a combine keeps an existing value. --- src/psbt.c | 3 +++ src/test/test_psbt.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/psbt.c b/src/psbt.c index b6c316437..1f7fbfb90 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -3962,6 +3962,9 @@ static int psbt_combine(struct wally_psbt *psbt, const struct wally_psbt *src, #ifdef BUILD_ELEMENTS if (ret == WALLY_OK && is_pset) { psbt->pset_modifiable_flags |= src->pset_modifiable_flags; + if (mem_is_zero(psbt->genesis_blockhash, sizeof(psbt->genesis_blockhash))) + memcpy(psbt->genesis_blockhash, src->genesis_blockhash, + sizeof(psbt->genesis_blockhash)); ret = wally_map_combine(&psbt->global_scalars, &src->global_scalars); } #endif /* BUILD_ELEMENTS */ diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index ad3e41433..29a92b090 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -76,6 +76,38 @@ def test_set_version_without_tx(self): self.assertEqual(wally_psbt_get_version(psbt), (WALLY_OK, 0)) wally_psbt_free(psbt) + def test_pset_genesis_blockhash(self): + """Cloning or combining a PSET keeps its genesis blockhash""" + if not wally_is_elements_build()[1]: + self.skipTest('Requires an elements build') + genesis, genesis_len = make_cbuffer('77' * 32) + out, out_len = make_cbuffer('00' * 32) + + def get_genesis(psbt): + ret, written = wally_psbt_get_global_genesis_blockhash(psbt, out, out_len) + self.assertEqual(ret, WALLY_OK) + return bytes(out[:written]) + + src, empty, clone = [pointer(wally_psbt()) for _ in range(3)] + for psbt in [src, empty]: + self.assertEqual(wally_psbt_init_alloc(2, 0, 0, 0, INIT_PSET, psbt), WALLY_OK) + self.assertEqual(wally_psbt_set_global_genesis_blockhash(src, genesis, genesis_len), + WALLY_OK) + self.assertEqual(wally_psbt_clone_alloc(src, 0, clone), WALLY_OK) + self.assertEqual(get_genesis(clone), bytes(genesis)) + self.assertEqual(self.to_base64(clone), self.to_base64(src)) + # Combining fills in a missing hash and keeps an existing one + self.assertEqual(get_genesis(empty), b'') + self.assertEqual(wally_psbt_combine(empty, src), WALLY_OK) + self.assertEqual(get_genesis(empty), bytes(genesis)) + other, other_len = make_cbuffer('88' * 32) + self.assertEqual(wally_psbt_set_global_genesis_blockhash(src, other, other_len), + WALLY_OK) + self.assertEqual(wally_psbt_combine(empty, src), WALLY_OK) + self.assertEqual(get_genesis(empty), bytes(genesis)) + for psbt in [src, empty, clone]: + wally_psbt_free(psbt) + def test_valid(self): """Test deserializing and roundtripping valid PSBTs""" buf, buf_len = make_cbuffer('00' * 4096)