Merge bitcoin/bitcoin#36342: wallet: reject recipient amounts above MAX_MONEY
What changed, and why it matters
This patch fixes a bug in Bitcoin Core's wallet transaction creation where extremely large output amounts could cause an integer overflow or even crash the node. Before the fix, a user could craft a transaction whose outputs added up to more than the maximum allowed bitcoin amount (about 21 million BTC), leading to incorrect math, a failed internal assertion, or a successful but invalid transaction. The fix now rejects such oversized or overflowing amounts early with a clear error message.
Apply the patch. The fix is small, well-tested, and prevents a reachable crash/overflow in wallet RPCs. No additional immediate action is required beyond normal backporting to maintained release branches.
Security signals we found
Integer overflow in recipient amount summation
Node abort via failed assertion in coin selection
Possible creation of a transaction with wrapped/invalid total output value
Missing upper-bound validation on user-supplied output amounts
Functional test explicitly demonstrates crash/invalid-result scenarios on master
Evidence from the diff
CreateTransactionInternal in src/wallet/spend.cpp now validates each recipient amount and the running recipients_sum against MoneyRange before adding to the sum. This prevents int64 overflow and amounts exceeding MAX_MONEY. The separate negative-amount check in CreateTransaction is removed because MoneyRange also rejects negatives. A new functional test covers a single output above MAX_MONEY, multiple outputs whose sum exceeds MAX_MONEY, an int64-overflowing sum, and a negative output.
Changed components
src/wallet/spend.cppwallet transaction creation (CreateTransaction / CreateTransactionInternal)fundrawtransaction RPCwalletcreatefundedpsbt RPCsend/sendmany wallet RPCsInspect captured patch +25 / −4
### src/wallet/spend.cpp
@@ -1091,6 +1091,9 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
ReserveDestination reservedest(&wallet, change_type);
unsigned int outputs_to_subtract_fee_from = 0; // The number of outputs which we are subtracting the fee from
for (const auto& recipient : vecSend) {
+ if (!MoneyRange(recipient.nAmount) || !MoneyRange(recipients_sum + recipient.nAmount)) {
+ return util::Error{_("Transaction output amount must be valid")};
+ }
if (IsDust(recipient, wallet.chain().relayDustFee())) {
return util::Error{_("Transaction amount too small")};
}
@@ -1456,10 +1459,6 @@ util::Result<CreatedTransactionResult> CreateTransaction(
return util::Error{_("Transaction must have at least one recipient")};
}
- if (std::any_of(vecSend.cbegin(), vecSend.cend(), [](const auto& recipient){ return recipient.nAmount < 0; })) {
- return util::Error{_("Transaction amounts must not be negative")};
- }
-
LOCK(wallet.cs_wallet);
auto res = CreateTransactionInternal(wallet, vecSend, change_pos, coin_control, sign);
### test/functional/wallet_fundrawtransaction.py
@@ -16,6 +16,7 @@
COIN,
CTransaction,
CTxOut,
+ MAX_MONEY,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
@@ -158,9 +159,30 @@ def run_test(self):
self.test_feerate_rounding()
self.test_input_confs_control()
self.test_duplicate_outputs()
+ self.test_outputs_above_max_money()
self.test_watchonly_cannot_grind_r()
self.test_cannot_cover_fees()
+ def test_outputs_above_max_money(self):
+ self.log.info("Test funding a transaction whose outputs exceed MAX_MONEY")
+ spk = bytearray(address_to_scriptpubkey(self.nodes[0].getnewaddress()))
+ err = "Transaction output amount must be valid"
+
+ tx = CTransaction()
+ tx.vout = [CTxOut(MAX_MONEY + 1, spk)]
+ assert_raises_rpc_error(-4, err, self.nodes[0].fundrawtransaction, tx.serialize().hex())
+
+ # Each output is valid on its own, but the sum is above MAX_MONEY
+ tx.vout = [CTxOut(MAX_MONEY, spk), CTxOut(1, spk)]
+ assert_raises_rpc_error(-4, err, self.nodes[0].fundrawtransaction, tx.serialize().hex())
+
+ # The sum overflows int64
+ tx.vout = [CTxOut(2**62, spk)] * 2
+ assert_raises_rpc_error(-4, err, self.nodes[0].fundrawtransaction, tx.serialize().hex())
+
+ tx.vout = [CTxOut(-1, spk)]
+ assert_raises_rpc_error(-4, err, self.nodes[0].fundrawtransaction, tx.serialize().hex())
+
def test_duplicate_outputs(self):
self.log.info("Test deserializing and funding a transaction with duplicate outputs")
self.nodes[1].createwallet("fundtx_duplicate_outputs")Why this scored 62/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.