Repository navigation
Conversation
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.
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.
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.
Contributor
|
Merged with minor changes, many thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three small PSBT fixes for 1.6.1, targeting
post_1_6_0_updatesas asked in#556. Each is its own commit with a test that fails without the fix.
psbt: Refuse v2 global tx under loose parsing
wally_psbt_from_bytes()withWALLY_PSBT_PARSE_FLAG_LOOSEaccepted a v2 PSBTcarrying
PSBT_GLOBAL_UNSIGNED_TXwhen the tx key came before the version key.The input and output arrays allocated for the tx were then overwritten by the
v2 path and leaked. BIP-370
says "PSBT_GLOBAL_UNSIGNED_TX must be excluded in PSBTv2",
psbt_is_valid()already rejects such a PSBT so it could not be serialized, and the reverse key
order was already refused.
The parser now refuses both orders.
Test:
test_v2_global_tx_looseparses the version alone (accepted) and the txbefore and after the version (both refused).
psbt: Refuse upgrading a v0 PSBT with no tx
wally_psbt_set_version(psbt, 0, 2)on a v0 PSBT with no global tx readpsbt->txwithout checking it and crashed. This is worse than I described in#556: it does not need loose parsing. A v0 PSBT with no tx is valid and
wally_psbt_init_alloc(0, ...)returns one, so init then set_version crashes.The upgrade now returns
WALLY_EINVAL, the same waypsbt_build_tx()and theother v0 paths treat a missing tx.
Test:
test_set_version_without_txcovers both thewally_psbt_init_alloc()PSBT and the loosely parsed
psbt\xff\x00, and checks that a no-opset_version to 0 still succeeds and the version is unchanged after the refusal.
psbt: Copy the PSET genesis blockhash
wally_psbt_clone_alloc()andwally_psbt_combine()copied every global PSETfield except the genesis blockhash. A cloned PSET serialized without it, and
its taproot inputs could not be signed since the Elements taproot signature
hash requires it. It is now copied when the destination has none.
Test:
test_pset_genesis_blockhashchecks the clone serializes identically tothe source, that combine fills in a missing hash, and that combine keeps an
existing one.
Not included
wally_psbt_signing_cache_enable()alreadydocuments that it must be called again after any change that affects
signatures, so I left it as is.
wally_psbt_combine()keeps only the destination's taproot leaf signatures,leaf scripts and taproot derivations when both PSBTs have some, so two
co-signers of one tapscript leaf lose a signature on combine. That code is
being reworked in Tapscript & MuSig2 support, with a miniscript decoder and satisfier #535, so I have not touched it here.
Testing
src/test/test_psbt.pyandsrc/ctest/test_psbtpass on each commit. On thetip I ran, in Debian trixie on arm64: the
--enable-elements=no-Werrorbuild, clang scan-build, ASan/UBSan
make checkwith and without Elements, andvalgrind on the C tests plus
test_psbt.py. arm64 gcc needs-flax-vector-conversions -fsigned-charto build master at all (scrypt NEONand the ccan base64 table), so those two flags were added to match x86 CI.