Skip to content

Commit

Permalink
Merge pull request btcsuite#196 from Crypt-iQ/psbt_panic_fix_05142021
Browse files Browse the repository at this point in the history
psbt: bounds check SumUtxoInputValues with NonWitness.TxOut indexing
  • Loading branch information
Roasbeef committed May 14, 2021
2 parents a53e384 + 9c91ffc commit faeebcb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions psbt/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ func SumUtxoInputValues(packet *Packet) (int64, error) {
// the UTXO resides in.
utxOuts := in.NonWitnessUtxo.TxOut
txIn := packet.UnsignedTx.TxIn[idx]

// Check that utxOuts actually has enough space to
// contain the previous outpoint's index.
opIdx := txIn.PreviousOutPoint.Index
if opIdx >= uint32(len(utxOuts)) {
return 0, fmt.Errorf("input %d has malformed "+
"TxOut field", idx)
}

inputSum += utxOuts[txIn.PreviousOutPoint.Index].Value

default:
Expand Down
18 changes: 18 additions & 0 deletions psbt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ func TestSumUtxoInputValues(t *testing.T) {
if sum != (1234 + 6543) {
t.Fatalf("unexpected sum, got %d wanted %d", sum, 1234+6543)
}

// Create a malformed packet where NonWitnessUtxo.TxOut does not
// contain the index specified by the PreviousOutPoint in the
// packet's Unsigned.TxIn field.
badOp := []*wire.OutPoint{{}, {Index: 500}}
malformedPacket, err := New(badOp, nil, 2, 0, []uint32{0, 0})
if err != nil {
t.Fatalf("could not create malformed packet: %v", err)
}
malformedPacket.Inputs[0].WitnessUtxo = &wire.TxOut{Value: 1234}
malformedPacket.Inputs[1].NonWitnessUtxo = &wire.MsgTx{
TxOut: []*wire.TxOut{{Value: 6543}},
}

_, err = SumUtxoInputValues(malformedPacket)
if err == nil {
t.Fatalf("expected sum of malformed packet to fail")
}
}

func TestTxOutsEqual(t *testing.T) {
Expand Down

0 comments on commit faeebcb

Please sign in to comment.