Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/node/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
}

// Check if it is final
if (!utxo.IsNull() && !PSBTInputSigned(input)) {
if (!PSBTInputSignedAndVerified(psbtx, i, &txdata)) {
input_analysis.is_final = false;

// Figure out what is missing
Expand Down
29 changes: 28 additions & 1 deletion src/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ void PSBTOutput::Merge(const PSBTOutput& output)
if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
}

bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
{
CTxOut utxo;
assert(psbt.inputs.size() >= input_index);
const PSBTInput& input = psbt.inputs[input_index];

if (input.non_witness_utxo) {
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
COutPoint prevout = psbt.tx->vin[input_index].prevout;
if (prevout.n >= input.non_witness_utxo->vout.size()) {
return false;
}
if (input.non_witness_utxo->GetHash() != prevout.hash) {
return false;
}
utxo = input.non_witness_utxo->vout[prevout.n];
} else {
return false;
}

if (txdata) {
return VerifyScript(input.final_script_sig, utxo.scriptPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL});
} else {
return VerifyScript(input.final_script_sig, utxo.scriptPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, MissingDataBehavior::FAIL});
}
}
Comment on lines +184 to +209
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Critical: Off-by-one error in bounds assertion.

Line 187 uses >= which allows input_index == psbt.inputs.size(), but the subsequent access at line 188 would be out-of-bounds.

Apply this diff to fix the assertion:

-    assert(psbt.inputs.size() >= input_index);
+    assert(input_index < psbt.inputs.size());

Additionally, as noted in the header review, the function signature passes psbt by value which is inefficient. Consider using const PartiallySignedTransaction& psbt instead.

🤖 Prompt for AI Agents
In src/psbt.cpp around lines 184 to 209, the bounds assertion is inverted and
allows an out-of-range access; change the assertion to ensure input_index is
strictly less than psbt.inputs.size() (e.g. assert(input_index <
psbt.inputs.size())) and update the function signature to take the PSBT by const
reference (const PartiallySignedTransaction& psbt) to avoid unnecessary copying;
make corresponding symbol/name adjustments in the function body if needed.


size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
size_t count = 0;
for (const auto& input : psbt.inputs) {
Expand Down Expand Up @@ -238,7 +265,7 @@ bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction&
PSBTInput& input = psbt.inputs.at(index);
const CMutableTransaction& tx = *psbt.tx;

if (PSBTInputSigned(input)) {
if (PSBTInputSignedAndVerified(psbt, index, txdata)) {
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,12 @@ std::string PSBTRoleName(PSBTRole role);
/** Compute a PrecomputedTransactionData object from a psbt. */
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt);

/** Checks whether a PSBTInput is already signed. */
/** Checks whether a PSBTInput is already signed by checking for non-null finalized fields. */
bool PSBTInputSigned(const PSBTInput& input);

/** Checks whether a PSBTInput is already signed by doing script verification using final fields. */
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata);

/** Signs a PSBTInput, verifying that all provided data matches what is being signed.
*
* txdata should be the output of PrecomputePSBTData (which can be shared across
Expand Down
1 change: 1 addition & 0 deletions test/functional/rpc_psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,5 +504,6 @@ def test_psbt_input_keys(psbt_input, keys):
self.log.info("Test we don't crash when making a 0-value funded transaction at 0 fee without forcing an input selection")
assert_raises_rpc_error(-4, "Transaction requires one destination of non-0 value, a non-0 feerate, or a pre-selected input", self.nodes[0].walletcreatefundedpsbt, [], [{"data": "deadbeef"}], 0, {"fee_rate": "0"})


if __name__ == '__main__':
PSBTTest().main()
Loading