Skip to content

Conversation

achow101
Copy link
Member

@achow101 achow101 commented Jun 8, 2020

Due to recent changes to hardware wallets, the full previous transaction will need to be provided for segwit inputs. Since some software may be checking for the existence of a witness_utxo to determine whether to produce a segwit signature, we keep that field to ease the transition.

Because all of the sanity checks implemented by the IsSane functions were related to having mixed segwit and non-segwit data in a PSBT, those functions are removed as those checks are no longer proper.

Some tests are updated/removed to accommodate this and a simple test added to check that both UTXOs are being added to segwit inputs.

As discussed in the wallet IRC meeting, our own signer will not require non_witness_utxo for segwit inputs.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 8, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Rspigler
Copy link
Contributor

Rspigler commented Jun 9, 2020

Could you explain further what you mean by including non_witness_utxo but not requiring it for signing?

@achow101
Copy link
Member Author

achow101 commented Jun 9, 2020

Could you explain further what you mean by including non_witness_utxo but not requiring it for signing?

non_witness_utxo will be added to segwit inputs during updating. However Core's signer will not enforce that it exists for segwit inputs during signing - witness_utxo can be provided instead. I.e. signing will not fail if a segwit input only has a segwit_utxo.

Copy link
Member

@luke-jr luke-jr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 12, 2020
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 12, 2020
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 12, 2020
Offline signers will always need a non_witness_utxo so make sure it is
there.

Github-Pull: bitcoin#19215
Rebased-From: a656eb3
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 12, 2020
@Sjors
Copy link
Member

Sjors commented Jun 17, 2020

I tested 836d6fc with a Trezor, Ledger Nano X and Nano S that this doesn't break old firmware and seems to work nicely with new firmware, see bitcoin-core/HWI#340

achow101 added a commit to bitcoin-core/HWI that referenced this pull request Jun 22, 2020
1613a33 trezorlib: Raise a more specific error when a prevtx is not provided (Andrew Chow)
45dbd08 ledger: For app v1.4.0+, do getTrustedInput on segwit inputs (Andrew Chow)
fe0f82a btchip: Give the major, minor, and patch versions separately (Andrew Chow)
ce5e095 btchip: prefer trustedInput over witness (Andrew Chow)
9800899 ledger: Determine segwit based on scripts and allow both UTXOs (Andrew Chow)
c085077 tests: increase signtx keypool (Andrew Chow)
a47f7e5 trezor: Determine segwit based on scripts instead of provided info (Andrew Chow)
662eab0 bitbox: Update to inspect scriptcode for signature type (Andrew Chow)
249825d Make imports multiline (Andrew Chow)
9f8e03c trezorlib: Update sign_tx from upstream (Andrew Chow)
a264d84 test: Do legacy, then segwit, then mixed (Andrew Chow)
4ba72f3 trezor: handle when psbt has both types of UTXOs (Andrew Chow)
2f40d0f trezor: Use standard derivation paths (Andrew Chow)
f862135 trezor: Set coin name as testnet when --testnet (Andrew Chow)
d662fad serializations: Refactor script type matching into separate functions (Andrew Chow)
3b6131c test: Patch bitcoind to give out PSBTs with both UTXOs (Andrew Chow)
65c6de6 psbt: Allow PSBTs to have both non_witness_utxo and witness_utxo (Andrew Chow)

Pull request description:

  This is a general update to fix many issues related to newly released signing behavior in Trezors and Ledgers.

  * The PSBT module is updated to handle PSBT inputs that include both `non_witness_utxo` and `witness_utxo`
  * For Trezor, Ledger, and Bitbox, instead of checking for existence of `witness_utxo`, `witness_script`, and `redeem_script` to determine signing behavior, we instead inspect the scripts using existing template functions to determine their type and base our behavior on that.
  * For Trezor and Ledger, the signing behavior is updated to pass in the full previous transaction for segwit inputs if they are available.
  * For Trezor, all of the derivation paths are updated to fit within Trezor's "known paths".
  * In the tests, a patch to bitcoind is added that allows for PSBTs with inputs that have both UTXO types. This is mostly from bitcoin/bitcoin#19215 and should be removed once that PR is merged.

  Note that the Ledger emulator has not been updated to include the Bitcoin app that has the segwit requirements. Also note that the updated app with those requirements seems to have a [bug](LedgerHQ/app-bitcoin#154) when a transaction has multiple segwit inputs.

  Closes #338 and #337

ACKs for top commit:
  Sjors:
    Tested and rather cursory code review ACK 1613a33. I didn't test the BitBox changes.

Tree-SHA512: 18e238748e4e17cc692c2725881b62dc13499d127b532922b550cf41bd3c128927520f91b560706c70b4f4059efefccc3e1fe103168340ecb7eb5bb0060f1bfc
@sipa
Copy link
Member

sipa commented Jun 29, 2020

utACK. I think we'll want this for 0.20.1

@maflcko maflcko added this to the 0.20.1 milestone Jun 29, 2020
@maflcko
Copy link
Member

maflcko commented Jun 29, 2020

@luke-jr @Sjors mind to re-ACK?

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 84d295e, but I'm fuzzy on motivation for this change. I can vaguely see how not including full utxo transaction and signing offline might not be safe, but not sure what in segwit v1 will make it safe. Would be good to clarify this in one of the TODO comments.

@@ -136,8 +136,8 @@ void PSBTInput::Merge(const PSBTInput& input)
{
if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
// TODO: For segwit v1, we will want to clear out the non-witness utxo when setting a witness one. For v0 and non-segwit, this is not safe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "psbt: always put a non_witness_utxo and don't remove it" (4600479)

Can this comment be elaborated a little? I haven't been following this closely and don't know what in segwit v1 makes it safe to clear the non_witness_utxo transaction when signing offline.

fanquake added a commit that referenced this pull request Jul 10, 2020
2b79ac7 Clean up separated ban/discourage interface (Pieter Wuille)
0477348 Replace automatic bans with discouragement filter (Pieter Wuille)
e7f06f9 test: remove Cirrus CI FreeBSD job (fanquake)
eb6b82a qa: Test concurrent wallet loading (João Barbosa)
c9b49d2 wallet: Handle concurrent wallet loading (João Barbosa)
cf0b5a9 tests: Check that segwit inputs in psbt have both UTXO types (Andrew Chow)
3228b59 psbt: always put a non_witness_utxo and don't remove it (Andrew Chow)
ed5ec30 psbt: Allow both non_witness_utxo and witness_utxo (Andrew Chow)
68e0e6f rpc: show both UTXOs in decodepsbt (Andrew Chow)
27786d0 trivial: Suggested cleanups to surrounding code (Russell Yanofsky)
654420d wallet: Minimal fix to restore conflicted transaction notifications (Russell Yanofsky)
febebc4 Fix WSL file locking by using flock instead of fcntl (Samuel Dobson)
5c7151a gui: update Qt base translations for macOS release (fanquake)
c219d21 build: improved output of configure for build OS (sachinkm77)
0596a6e util: Don't reference errno when pthread fails. (MIZUTA Takeshi)

Pull request description:

  Currently backports the following to the 0.20 branch:
  * #18700 - Fix locking on WSL using flock instead of fcntl
  * #18982 - wallet: Minimal fix to restore conflicted transaction notifications
  * #19059 - gui: update Qt base translations for macOS release
  * #19152 - build: improve build OS configure output
  * #19194 -  util: Don't reference errno when pthread fails.
  * #19215 - psbt: Include and allow both non_witness_utxo and witness_utxo for segwit inputs
  * #19219 - Replace automatic bans with discouragement filter
  * #19300 - wallet: Handle concurrent wallet loading

ACKs for top commit:
  amitiuttarwar:
    ACK 0477348 2b79ac7 by comparing to original changes, double checking the diff
  sipa:
    utACK 2b79ac7
  laanwj:
    ACK 2b79ac7

Tree-SHA512: e5eb31d08288fa4a6e8aba08e60b16ce1988f14f249238b1cfd18ab2c8f6fcd9f07e3c0c491d32d2361fca26e3037fb0374f37464bddcabecea29069d6737539
@bitcoinhodler
Copy link
Contributor

Is there any way to get walletprocesspsbt not to add the non_witness_utxo now?

My use case is offline signing of PSBTs via printed QR codes, and this makes that much more difficult since the PSBTs can be so much bigger now.

I understand why Trezor requires this, but I'm not concerned about that attack vector.

@Sjors
Copy link
Member

Sjors commented Jul 14, 2020

@bitcoinhodler that could be achieved by adding an argument to that RPC call. For the GUI that would cause additional clutter, but perhaps an "advanced" section in the Send or PSBT dialog is useful anyway.

@@ -1121,7 +1122,9 @@ UniValue decodepsbt(const JSONRPCRequest& request)
ScriptToUniv(txout.scriptPubKey, o, true);
out.pushKV("scriptPubKey", o);
in.pushKV("witness_utxo", out);
} else if (input.non_witness_utxo) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "rpc: show both UTXOs in decodepsbt" (72f6bec)

Note: Bug here is fixed in #19517 (changing else if to if can allow total_in to be incremented twice with the same amount)

guggero added a commit to guggero/btcutil that referenced this pull request Jul 20, 2020
As described in CVE-2020-14199 it is unsafe to only rely on witness
UTXO information when signing. Hardware wallets fixed this by also
requiring the full non-witness UTXO to be present for a witness input.
To be compatible with those newer hardware wallet firmware, we need to
remove the sanity checks that disallowed setting witness and non-witness
UTXOs at the same time.
See bitcoin/bitcoin#19215 for comparison which
removed the sanity checks in Bitcoin Core.
laanwj added a commit that referenced this pull request Aug 1, 2020
7c1c153 doc: Update 0.20.1 release notes with psbt changes (Andrew Chow)

Pull request description:

  #19215 was missing from the list. Also felt it was important to mention this change.

Top commit has no ACKs.

Tree-SHA512: b795cf73954ff493747a793039918a5e19c377d9325e6156a8e23ba8f510af12f48b2d63854f57d482640531a865190f1fe1ece0c78a5e45d6926f9533c6d695
backpacker69 referenced this pull request in peercoin/peercoin Sep 8, 2020
Github-Pull: #19215
Rebased-From: 72f6bec
backpacker69 referenced this pull request in peercoin/peercoin Sep 8, 2020
backpacker69 referenced this pull request in peercoin/peercoin Sep 8, 2020
Offline signers will always need a non_witness_utxo so make sure it is
there.

Github-Pull: #19215
Rebased-From: 4600479
backpacker69 referenced this pull request in peercoin/peercoin Sep 8, 2020
Bushstar pushed a commit to Bushstar/omnicore that referenced this pull request Oct 21, 2020
Bushstar pushed a commit to Bushstar/omnicore that referenced this pull request Oct 21, 2020
Bushstar pushed a commit to Bushstar/omnicore that referenced this pull request Oct 21, 2020
Offline signers will always need a non_witness_utxo so make sure it is
there.

Github-Pull: bitcoin#19215
Rebased-From: 4600479
Bushstar pushed a commit to Bushstar/omnicore that referenced this pull request Oct 21, 2020
bitcoinhodler added a commit to bitcoinhodler/GlacierProtocol that referenced this pull request Oct 25, 2020
See: bitcoin/bitcoin#19215

As long as the user doesn't sign twice, the security issue fixed by
that PR is not an issue.

In Glacier, it would be really, really hard to accidentally sign two
subtly different transactions.

Without this change, all the self-checking PSBT tests would start
failing in Bitcoin Core 0.21, because online_regtest_wallet.py expects
the PSBTs to be the same as it created last time. Instead they will
now include both witness and non-witness UTXO data.
bitcoinhodler added a commit to bitcoinhodler/GlacierProtocol that referenced this pull request Oct 25, 2020
See: bitcoin/bitcoin#19215

As long as the user doesn't sign twice, the security issue fixed by
that PR is not an issue.

In Glacier, it would be really, really hard to accidentally sign two
subtly different transactions.

Without this change, all the self-checking PSBT tests would start
failing in Bitcoin Core 0.21, because online_regtest_wallet.py expects
the PSBTs to be the same as it created last time. Instead they would
now include both witness and non-witness UTXO data.
bitcoinhodler added a commit to bitcoinhodler/GlacierProtocol that referenced this pull request Oct 25, 2020
See: bitcoin/bitcoin#19215

As long as the user doesn't sign twice, the security issue fixed by
that PR is not an issue.

In Glacier, it would be really, really hard to accidentally sign two
subtly different transactions.

Without this change, all the self-checking PSBT tests would start
failing in Bitcoin Core 0.21, because online_regtest_wallet.py expects
the PSBTs to be the same as it created last time. Instead they would
now include both witness and non-witness UTXO data.
bitcoinhodler added a commit to bitcoinhodler/GlacierProtocol that referenced this pull request Oct 25, 2020
See: bitcoin/bitcoin#19215

As long as the user doesn't sign twice, the security issue fixed by
that PR is not an issue.

In Glacier, it would be really, really hard to accidentally sign two
subtly different transactions.

Without this change, all the self-checking PSBT tests would start
failing in Bitcoin Core 0.21, because online_regtest_wallet.py expects
the PSBTs to be the same as it created last time. Instead they would
now include both witness and non-witness UTXO data.
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 2, 2021
…gwit inputs

Summary:
Backport of core [[bitcoin/bitcoin#19215 | PR19215]].

The relevant commits are:
 - bitcoin/bitcoin@5279d8b
 - bitcoin/bitcoin@84d295e

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9128
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.