← Socratic Seminar #52 ← → or space to navigate

Bitcoin Core #36342 · merged 2026-11-20

Reject recipient amounts above MAX_MONEY

The wallet added up output amounts without a range check. Big enough outputs overflowed a 64-bit integer, crashing the node or making fundrawtransaction "succeed" with 46 billion BTC outputs.

Author: Bruce039 · github.com/bitcoin/bitcoin/pull/36342

Background

Amounts are signed 64-bit satoshis

  • CAmount is an int64_t counting satoshis. Its max is 263 − 1 ≈ 9.22 × 1018 sat.
  • MAX_MONEY = 21,000,000 BTC = 2.1 × 1015 sat, about 4,400× smaller than the int64 ceiling.
  • MoneyRange(v) means 0 ≤ v ≤ MAX_MONEY. Consensus code checks it per output and on the running total.
  • The wallet's CreateTransactionInternal only checked that amounts weren't negative, then did recipients_sum += recipient.nAmount with no range check.

The overflow

Two outputs of 262 sat wrap the sum negative

An int64 number line from -2^63 to 2^63-1. MAX_MONEY is a tiny sliver near zero. Adding 2^62 twice passes INT64_MAX and wraps around to -2^63. recipients_sum starts at 0, then adds each output: MAX_MONEY (21M BTC) is this sliver at 0 −2⁶³ 0 2⁶² 2⁶³ − 1 INT64_MAX + 2⁶² (output 1) + 2⁶² (output 2) 2⁶³ overflows → wraps to −2⁶³

Signed overflow is undefined behavior in C++; UBSan flags it at the recipients_sum += line. In practice it wraps around.

What happened on master

Crash, wrong error, or fake success

Sum wraps negative

Two outputs of 262 sat: the node aborts on assert(!m_selected_inputs.empty()) in RecalculateWaste.

Sum wraps small

Other values wrap to a small positive number, and fundrawtransaction returns success for outputs of 46,116,860,184 BTC each (262 sat).

One output > MAX_MONEY

No overflow, just a misleading "Insufficient funds" error instead of "invalid amount".

The "Transaction too large" check only runs after coin selection, so it never got a chance to stop this.

How to reach it

Raw amounts, or just a lot of valid ones

  • Easiest: fundrawtransaction on a raw tx with two outputs of 262 sat each.
  • With only valid amounts: walletcreatefundedpsbt, send or sendmany with a few thousand outputs of 21M BTC each. About 4,393 of them pass 263.
  • It needs RPC access to the wallet, so this is a local crash and correctness bug, not a remote attack.

The fix

Range-check every amount and the running sum

// src/wallet/spend.cpp · CreateTransactionInternal()
     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())) {
     ...
// CreateTransaction(): old negative-only check removed, MoneyRange covers it
-    if (std::any_of(vecSend.cbegin(), vecSend.cend(),
-                    [](const auto& recipient){ return recipient.nAmount < 0; })) {
-        return util::Error{_("Transaction amounts must not be negative")};
-    }
  • The check runs before the add, so the sum can never leave [0, MAX_MONEY] and never overflows.
  • It fails before coin selection starts. New test covers > MAX_MONEY, a sum > MAX_MONEY, int64 overflow and a negative output.

Why no coins were at risk

Consensus already says no

// src/consensus/tx_check.cpp · CheckTransaction()
        if (txout.nValue > MAX_MONEY)
            return state.Invalid(..., "bad-txns-vout-toolarge");
        nValueOut += txout.nValue;
        if (!MoneyRange(nValueOut))
            return state.Invalid(..., "bad-txns-txouttotal-toolarge");
  • A "funded" 46-billion-BTC transaction could never be relayed or mined.
  • Discussion: consensus checks MoneyRange on every add; why didn't the wallet reuse the same pattern from the start?
  • Callers that build transactions from untrusted input: what else trusts amounts before coin selection?