psbt: Refuse upgrading a v0 PSBT with no tx
What changed, and why it matters
This commit fixes a crash bug in libwally-core, a Bitcoin/Elements cryptography library. When a user tried to upgrade an older-style PSBT (Partially Signed Bitcoin Transaction) to version 2, the code would follow a pointer to transaction data without first checking that the data existed. If the PSBT had no transaction attached, that pointer was NULL, causing the program to crash (NULL pointer dereference). The fix simply refuses the upgrade with an error when no transaction is present, matching how other parts of the code already handle the same situation.
Treat as a low-to-moderate reliability/defensive fix. Review callers of wally_psbt_set_version() to ensure they handle WALLY_EINVAL gracefully, and confirm no other v0-to-v2 upgrade paths dereference psbt->tx without a guard. Backport to maintained branches if crash exposure is reachable from untrusted PSBT input.
Security signals we found
NULL pointer dereference in transaction-version upgrade path
Missing input validation on internal object state before pointer dereference
Consistency fix aligning with existing psbt_build_tx() and v0 error handling
Regression test added for malformed/v0-no-tx upgrade attempt
Evidence from the diff
In wally_psbt_set_version(), upgrading a v0 PSBT to v2 dereferenced psbt->tx to read version, locktime, inputs and outputs without verifying psbt->tx was non-NULL. A v0 PSBT without a global tx is valid per the library’s own allocation and parsing paths, so set_version() could hit a NULL pointer dereference. The patch adds an explicit check: if psbt->version == PSBT_0 && !psbt->tx, return WALLY_EINVAL. A Python regression test nulls out the tx pointer and asserts WALLY_EINVAL is returned.
Changed components
src/psbt.cwally_psbt_set_version()PSBT v0 to v2 upgrade pathInspect captured patch +10 / −0
### src/psbt.c
@@ -4325,6 +4325,9 @@ int wally_psbt_set_version(struct wally_psbt *psbt,
if (psbt->version == version)
return WALLY_OK; /* No-op */
+ if (psbt->version == PSBT_0 && !psbt->tx)
+ return WALLY_EINVAL; /* No global tx to upgrade from */
+
if (wally_psbt_is_elements(psbt, &is_pset) != WALLY_OK || is_pset)
return WALLY_EINVAL; /* PSET only supports v2 */
### src/test/test_psbt.py
@@ -99,6 +99,13 @@ def test_valid(self):
post_hex = wally_tx_to_hex(tx, USE_WITNESS)[1]
self.assertEqual(pre_hex, post_hex)
+ if new_version == 0:
+ # Upgrading to v2 fails if no global tx is present
+ wally_tx_free(psbt.contents.tx)
+ psbt.contents.tx = None
+ self.assertEqual(wally_psbt_set_version(psbt, 0, 2),
+ WALLY_EINVAL)
+
wally_psbt_free(psbt)
def test_global_tx_malloc_fail(self):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.