From 1cf77a2dc36c81b80a2f9af52ed99bd426061de8 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 15 Oct 2019 17:15:22 -0400 Subject: Don't calculate tx fees for PSBTs with invalid money values In decodepsbt if an invalid amount is seen, don't calculate the fee but still show the invalid value in the decode. In analyze psbt, if an invalid amount is seen, set the next step to be the creator as the creator needs to remake the transaction so that it is valid. Github-Pull: #17156 Rebased-From: f1ef7f0aa46338f4cd8de79696027a1bf868f359 --- src/node/psbt.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/node') diff --git a/src/node/psbt.cpp b/src/node/psbt.cpp index 9a30c3f083..69fb1a28a9 100644 --- a/src/node/psbt.cpp +++ b/src/node/psbt.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include @@ -31,6 +32,10 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) // Check for a UTXO CTxOut utxo; if (psbtx.GetInputUTXO(utxo, i)) { + if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) { + result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i)); + return result; + } in_amt += utxo.nValue; input_analysis.has_utxo = true; } else { @@ -85,9 +90,16 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) // Get the output amount CAmount out_amt = std::accumulate(psbtx.tx->vout.begin(), psbtx.tx->vout.end(), CAmount(0), [](CAmount a, const CTxOut& b) { + if (!MoneyRange(a) || !MoneyRange(b.nValue) || !MoneyRange(a + b.nValue)) { + return CAmount(-1); + } return a += b.nValue; } ); + if (!MoneyRange(out_amt)) { + result.SetInvalid(strprintf("PSBT is not valid. Output amount invalid")); + return result; + } // Get the fee CAmount fee = in_amt - out_amt; -- cgit v1.2.3 From f5fb7fca969cd43318384bec46bb7687b1a529fd Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 15 Oct 2019 17:26:46 -0400 Subject: psbt: check output index is within bounds before accessing Github-Pull: #17156 Rebased-From: deaa6dd144f5650b385658a0c4f9a014aff8dde2 --- src/node/psbt.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/node') diff --git a/src/node/psbt.cpp b/src/node/psbt.cpp index 69fb1a28a9..ff7a38e40d 100644 --- a/src/node/psbt.cpp +++ b/src/node/psbt.cpp @@ -39,6 +39,10 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) in_amt += utxo.nValue; input_analysis.has_utxo = true; } else { + if (input.non_witness_utxo && psbtx.tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) { + result.SetInvalid(strprintf("PSBT is not valid. Input %u specifies invalid prevout", i)); + return result; + } input_analysis.has_utxo = false; input_analysis.is_final = false; input_analysis.next = PSBTRole::UPDATER; -- cgit v1.2.3