Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] lnd crashes when not correct psbt data is used as input #7527

Closed
ziggie1984 opened this issue Mar 20, 2023 · 5 comments · Fixed by #7529
Closed

[bug] lnd crashes when not correct psbt data is used as input #7527

ziggie1984 opened this issue Mar 20, 2023 · 5 comments · Fixed by #7529
Labels
bug Unintended code behaviour psbt taproot
Milestone

Comments

@ziggie1984
Copy link
Collaborator

Lnd will crash if the provided psbt (funded psbt) is missing the psbt NonWitnessUtxo or WitnessUtxo field. The problem lies here:
https://github.com/btcsuite/btcd/blob/master/txscript/hashcache.go#L239

We should check the psbt for this data to prevent any crashing of lnd.

I created the following psbt code which has missing utxo information and will therefore lead to an lnd crash

rlncli wallet psbt finalize cHNidP8BAF4CAAAAAQlokRq0L38emcB8Um/7+GXDR8gIJzlWRFOf4/c+xoaUAQAAAAD/////Ab/EPgUAAAAAIlEgfCPRPJzr08tF7NAdPvdArXQbVojQ+UkVL2LBAOgkWpgAAAAAAAAA

As already discussed offline we should solve this issue in lnd, meaning that we should check the psbt there before we attempt to calculate any sighashes.

Your environment

  • version of lnd: 0.16 rc3
@ziggie1984
Copy link
Collaborator Author

Part of the stack trace when finializing the psbt:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x104d216d4]
goroutine 482 [running]:
[github.com/btcsuite/btcd/txscript.NewTxSigHashes(0x140008ce380](http://github.com/btcsuite/btcd/txscript.NewTxSigHashes(0x140008ce380), {0x106480fe0, 0x14000140428})
	/Users/ziggie-pro/working_freetime/btcd/txscript/hashcache.go:243 +0x154
[github.com/btcsuite/btcwallet/wallet.(*Wallet).FinalizePsbt(0x1400037e000](http://github.com/btcsuite/btcwallet/wallet.(*Wallet).FinalizePsbt(0x1400037e000), 0x0, 0x0, 0x140000b5720)
	/Users/ziggie-pro/working_freetime/btcwallet/wallet/psbt.go:397 +0x68

@Roasbeef
Copy link
Member

Roasbeef commented Apr 6, 2023

Fixed by #7529

@Roasbeef Roasbeef closed this as completed Apr 6, 2023
@vindard
Copy link

vindard commented Apr 26, 2023

Out of curiosity, why would this txn

{
  "txid": "43c79b969d3e8e6cbe198f5b25e8cd03120372eab26578f486bc6258b8677a57",
  "hash": "653cc4921119c7f127bb56f125163d279376fd6fa0995f3dd91c2a51e8a108ea",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 200,
  "vin": [
    {
      "txid": "4853a89d083df152f697aa5d63bcd6919ffaa0213b7736d227eed10fd53c6378",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "304402202874d54141b3804267cec7320f4a27ddc22e1c17100c7106630ef40e1f01cb4402200527d5491d94887c7178b58dab73153ba5e027a225eb413f8ab660575ee4907401",
        "0384e78028f045c515e8730b67fa96d12e3bd2572d0f312c129872655747f19f40"
      ],
      "sequence": 4294967293
    }
  ],
  "vout": [
    {
      "value": 1.00000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 6f7ec5399599d8cfecb5bb5218bd85c6f419fec3",
        "desc": "addr(bcrt1qdalv2wv4n8vvlm94hdfp30v9cm6pnlkrqq5flr)#c6nl327t",
        "hex": "00146f7ec5399599d8cfecb5bb5218bd85c6f419fec3",
        "address": "bcrt1qdalv2wv4n8vvlm94hdfp30v9cm6pnlkrqq5flr",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 48.99997180,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 f2774e8e9922f3c728d2e30dfec006d8cd6fee53",
        "desc": "addr(bcrt1q7fm5ar5eyteuw2xjuvxlasqxmrxklmjnx83cat)#vt5cse08",
        "hex": "0014f2774e8e9922f3c728d2e30dfec006d8cd6fee53",
        "address": "bcrt1q7fm5ar5eyteuw2xjuvxlasqxmrxklmjnx83cat",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}

with this call

$ TX_ID="43c79b969d3e8e6cbe198f5b25e8cd03120372eab26578f486bc6258b8677a57"
$ PSBT=$(bitcoin-cli createpsbt "[{\"txid\":\"${TX_ID}\",\"vout\":0}]" "[{\"bcrt1q2xfvunp3hnl4yc2tn99epl27xqmxnxpp9st8ad\":0.5},{\"bcrt1qvlpe3vvtr7jxgkp5yyqsum795w03n8236uujql\":0.49}]")

$ lncli wallet psbt finalize $PSBT

produce this error for lnd?

[lncli] rpc error: code = Unknown desc = error finalizing PSBT: invalid PSBT, input with index 0 missing utxo information

Context

I was originally trying this on lnd 15 and running into the same errors ziggie fiest reported. And then when I bumped my docker images to 16 started getting these new errors.

Also below is the PSBT I'm trying to finalize

$ echo $PSBT
cHNidP8BAHECAAAAAVd6Z7hYYryG9HhlsupyAxIDzeglW48ZvmyOPp2Wm8dDAAAAAAD9////AoDw+gIAAAAAFgAUUZLOTDG8/1JhS5lLkP1eMDZpmCFArusCAAAAABYAFGfDmLGLH6RkWDQhAQ5vxaOfGZ1RAAAAAAAAAAA=

@guggero
Copy link
Collaborator

guggero commented Apr 26, 2023

You need to add the UTXO information to each input of the PSBT. You should be able to use bitcoin-cli utxoupdatepsbt to do that.

@vindard
Copy link

vindard commented Apr 26, 2023

This worked perfectly, thanks! I simply ran $ bitcoin-cli utxoupdatepsbt $PSBT and then used the output of that in lncli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unintended code behaviour psbt taproot
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants