psbt: Refuse v2 global tx under loose parsing
What changed, and why it matters
This change fixes a memory leak in the library's handling of a specific kind of invalid Bitcoin transaction file (PSBT v2 with an unexpected global transaction). Previously, parsing such a file under loose parsing could allocate memory for inputs/outputs, then overwrite those pointers, leaking the memory. The patch now rejects these invalid files outright. It does not appear to be a security-critical remote code execution or theft bug, but it is a correctness and denial-of-service hardening fix.
Apply the patch. It is a low-risk hardening fix that prevents memory leaks and rejects invalid PSBT v2 inputs. No immediate incident response is warranted unless the application is exposed to untrusted PSBT parsing and memory exhaustion is a concern, in which case updating and monitoring for malformed v2 PSBTs is prudent.
Security signals we found
Memory leak on parsing malformed PSBT v2 with global tx
Invalid PSBT v2 accepted under loose parsing flag
Pointer overwrite after allocation in psbt_set_global_tx / v2 path
New explicit validation: v2 PSBT must not carry global unsigned tx
Evidence from the diff
In wally_psbt_from_bytes() with WALLY_PSBT_PARSE_FLAG_LOOSE, a v2 PSBT containing PSBT_GLOBAL_UNSIGNED_TX was accepted if the tx key preceded the version key. psbt_set_global_tx() allocated input/output arrays, then the v2 path overwrote those pointers with new arrays, causing a memory leak. The patch adds an explicit check: if (*output)->tx is non-NULL when version is PSBT_2, return WALLY_EINVAL. This prevents both the leak and acceptance of an invalid v2 PSBT. The commit adds two test vectors covering both key orderings.
Changed components
src/psbt.cwally_psbt_from_bytes()PSBT v2 parsing pathWALLY_PSBT_PARSE_FLAG_LOOSE handlingInspect captured patch +17 / −1
### src/ctest/psbts.h
@@ -537,6 +537,12 @@ static const struct psbt_test invalid_psbts[] =
/* PSBT_IN_TAP_BIP32_DERIVATION too large (overflows) */
{"cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIIRYznOfhZeZ9k62z/viKbUvu0z8B+odvBaIlJCuCpjGrwBH/AAAAAAAAAAgAAAAAAAAAAAAAAA==", false, true},
+
+ /* PSBT v2 with global tx present (tx then psbt version) */
+ {"cHNidP8BAD0CAAAAARERERERERERERERERERERERERERERERERERERERERERAAAAAAD/////AQAAAAAAAAAAAVEAAAAAAfsEAgAAAAA=", false, true},
+
+ /* PSBT v2 with global tx present (psbt version then tx) */
+ {"cHNidP8B+wQCAAAAAQA9AgAAAAEREREREREREREREREREREREREREREREREREREREREREQAAAAAA/////wEAAAAAAAAAAAFRAAAAAAA=", false, true},
};
static const struct psbt_test valid_psbts[] =
### src/data/psbt.json
@@ -762,6 +762,14 @@
{
"comment": "PSBT_IN_TAP_BIP32_DERIVATION too large (overflows)",
"psbt": "cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIIRYznOfhZeZ9k62z/viKbUvu0z8B+odvBaIlJCuCpjGrwBH/AAAAAAAAAAgAAAAAAAAAAAAAAA=="
+ },
+ {
+ "comment": "PSBT v2 with global tx present (tx then psbt version)",
+ "psbt": "cHNidP8BAD0CAAAAARERERERERERERERERERERERERERERERERERERERERERAAAAAAD/////AQAAAAAAAAAAAVEAAAAAAfsEAgAAAAA="
+ },
+ {
+ "comment": "PSBT v2 with global tx present (psbt version then tx)",
+ "psbt": "cHNidP8B+wQCAAAAAQA9AgAAAAEREREREREREREREREREREREREREREREREREREREREREQAAAAAA/////wEAAAAAAAAAAAFRAAAAAAA="
}
],
"valid": [
### 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;Why this scored 45/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.