wallet: reject recipient amounts above MAX_MONEY #36342

Manually merged
Bruce039 merged 1 commit from fix-recipients-sum-overflow into master 2026-11-20 23:24:12 +00:00 AGit
Bruce039 commented 2026-09-26 09:53:11 +00:00 (Migrated from github.com)

CreateTransaction checks that recipient amounts aren't negative, but not that they're within MAX_MONEY. CreateTransactionInternal then sums them into recipients_sum (spend.cpp:1100) with no range check, and that sum can overflow.

Easiest way to hit it is fundrawtransaction with a raw tx that has two outputs of 2^62 sat. It can also be reached with only valid amounts through walletcreatefundedpsbt/send/sendmany and a few thousand 21M BTC outputs. The "Transaction too large" check only runs after coin selection, so it doesn't stop this. On master the node aborts:

Assertion failed: (!m_selected_inputs.empty()), function RecalculateWaste, file coinselection.cpp, line 904.

UBSan reports the overflow at spend.cpp:1100 first. With other values the sum wraps to a small positive number instead, and fundrawtransaction returns success for a tx with outputs of 46116860184 BTC each.

This checks every amount and the running sum against MoneyRange in the existing recipients loop of CreateTransactionInternal, before the amount is added to recipients_sum, and returns "Transaction output amount must be valid" before coin selection starts. The same check rejects negative amounts, so the separate negative check in CreateTransaction is removed. The new test_outputs_above_max_money in wallet_fundrawtransaction.py covers a single output above MAX_MONEY, two outputs whose sum is above it, a sum that overflows int64, and a negative output. On master the first case returns "Insufficient funds" and the overflow case crashes the node; with the fix all of them get the new error. wallet_fundrawtransaction.py, wallet_send.py, wallet_basic.py and rpc_psbt.py pass.

`CreateTransaction` checks that recipient amounts aren't negative, but not that they're within `MAX_MONEY`. `CreateTransactionInternal` then sums them into `recipients_sum` (spend.cpp:1100) with no range check, and that sum can overflow. Easiest way to hit it is `fundrawtransaction` with a raw tx that has two outputs of 2^62 sat. It can also be reached with only valid amounts through `walletcreatefundedpsbt`/`send`/`sendmany` and a few thousand 21M BTC outputs. The "Transaction too large" check only runs after coin selection, so it doesn't stop this. On master the node aborts: ``` Assertion failed: (!m_selected_inputs.empty()), function RecalculateWaste, file coinselection.cpp, line 904. ``` UBSan reports the overflow at spend.cpp:1100 first. With other values the sum wraps to a small positive number instead, and `fundrawtransaction` returns success for a tx with outputs of 46116860184 BTC each. This checks every amount and the running sum against `MoneyRange` in the existing recipients loop of `CreateTransactionInternal`, before the amount is added to `recipients_sum`, and returns "Transaction output amount must be valid" before coin selection starts. The same check rejects negative amounts, so the separate negative check in `CreateTransaction` is removed. The new `test_outputs_above_max_money` in `wallet_fundrawtransaction.py` covers a single output above MAX_MONEY, two outputs whose sum is above it, a sum that overflows int64, and a negative output. On master the first case returns "Insufficient funds" and the overflow case crashes the node; with the fix all of them get the new error. `wallet_fundrawtransaction.py`, `wallet_send.py`, `wallet_basic.py` and `rpc_psbt.py` pass.
DrahtBot commented 2026-09-26 09:53:19 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK maflcko, Zippo76, furszy, achow101

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36342 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36342.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36342#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [maflcko](https://github.com/bitcoin/bitcoin/pull/36342#issuecomment-6705616785), [Zippo76](https://github.com/bitcoin/bitcoin/pull/36342#pullrequestreview-5815262775), [furszy](https://github.com/bitcoin/bitcoin/pull/36342#pullrequestreview-5833329378), [achow101](https://github.com/bitcoin/bitcoin/pull/36342#pullrequestreview-6000659772) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
pinheadmz commented 2026-09-26 11:40:32 +00:00 (Migrated from github.com)

You just opened three PRs in ten minutes. Do all three of these conform to our AI Policy? Before we spend any precious human resources reading through your submissions can you please assure us that you understand all the choices you've made and that you are motivated by solving real problems real users have with the software?

You just opened three PRs in ten minutes. Do all three of these conform to our [AI Policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md)? Before we spend any precious human resources reading through your submissions can you please assure us that you understand all the choices you've made and that you are motivated by solving real problems real users have with the software?
Bruce039 commented 2026-09-26 12:05:57 +00:00 (Migrated from github.com)

Yes, I've read the AI policy and I accept it. And yes, I used AI, but only for the PR description and the tests. Finding the bug and the rest of the underlying work was done by me.
About the 3 PRs: I've been preparing for PRs for the last few days, and it made more sense to me to collect all the bugs I found and send them at the same time.

Yes, I've read the AI policy and I accept it. And yes, I used AI, but only for the PR description and the tests. Finding the bug and the rest of the underlying work was done by me. About the 3 PRs: I've been preparing for PRs for the last few days, and it made more sense to me to collect all the bugs I found and send them at the same time.
pinheadmz commented 2026-09-28 14:49:51 +00:00 (Migrated from github.com)

You now have 7 PRs open in two days, and you are a brand new contributor. If you really want to support the project I suggest closing 6 of them. Pick one that really means a lot to you, that you think is the most important for the most bitcoin users, and really commit to it. Show us that you the human cares about the project and is willing to put in the time and the hard work that the rest of us put in.

Earn your reputation here by starting slow.

We do not need another "AI operator" pasting claude output, that will result in a ban.

You now have 7 PRs open in two days, and you are a brand new contributor. If you really want to support the project I suggest closing 6 of them. Pick one that really means a lot to you, that you think is the most important for the most bitcoin users, and really commit to it. Show us that you the human cares about the project and is willing to put in the time and the hard work that the rest of us put in. Earn your reputation here by starting slow. We do not need another "AI operator" pasting claude output, that will result in a ban.
First-time contributor

🔵 Suggestion · An oversized total is reported as an invalid output amount

When individually in-range outputs total more than MAX_MONEY, fundrawtransaction reports an invalid output amount rather than explaining that the total exceeds the limit.

Location: src/wallet/spend.cpp:1094 (head).

🔵 Suggestion · The overflow test does not cover amount-check ordering

The first 2**62 output is rejected immediately, so this functional test would still pass if the running-sum expression were evaluated before validating the individual amount.

Location: test/functional/wallet_fundrawtransaction.py:180 (head).

Review coverage was incomplete.


Reviewed: bab579e56aa8 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:c11d2fb47a4274fd1fc74137130a218009e92047380936bbbd2bc5ff6ef4ce39:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:6c01bd0b2dc201c21e80ac3635828abf854ee5a0 --> <!-- ralph-head:bab579e56aa8b80a6f2923285e55cbcb2a7730b8 --> <!-- ralph-case:00ebd421aaa3362d9849827652360886e555aa38c6c283a4617e2382cb3ec5bd --> **🔵 Suggestion · An oversized total is reported as an invalid output amount** When individually in-range outputs total more than MAX_MONEY, fundrawtransaction reports an invalid output amount rather than explaining that the total exceeds the limit. Location: [`src/wallet/spend.cpp:1094`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bab579e56aa8b80a6f2923285e55cbcb2a7730b8/src/wallet/spend.cpp#L1094>) (head). **🔵 Suggestion · The overflow test does not cover amount-check ordering** The first 2**62 output is rejected immediately, so this functional test would still pass if the running-sum expression were evaluated before validating the individual amount. Location: [`test/functional/wallet_fundrawtransaction.py:180`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bab579e56aa8b80a6f2923285e55cbcb2a7730b8/test/functional/wallet_fundrawtransaction.py#L180>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`bab579e56aa8`](<https://git.fish.foo/bitcoin/bitcoin/commit/bab579e56aa8b80a6f2923285e55cbcb2a7730b8>) · [Full review report](<https://review.fish.foo/traces/36342-bab579e56aa8b80a6f2923285e55cbcb2a7730b8-1937-2.html>) · [Previous reports](<https://review.fish.foo/traces/36342/>) [permalink](<https://review.fish.foo/traces/36342-bab579e56aa8b80a6f2923285e55cbcb2a7730b8-1937-2.html>)
maflcko commented 2026-09-28 15:50:20 +00:00 (Migrated from github.com)

lgtm ACK f2915243d6

Seems fine as a low-prio sanity check and for nicer sanitizer use

lgtm ACK f2915243d616e9e579663cbea41e12f2ba1e93fa Seems fine as a low-prio sanity check and for nicer sanitizer use
maflcko (Migrated from github.com) reviewed 2026-09-29 08:34:36 +00:00
maflcko (Migrated from github.com) commented 2026-09-29 08:34:36 +00:00

nit: I think you can combine this check/loop and have the error message just be "Transaction output amount must be valid". this avoids two loops and overly strict hand holding for errors that should never happen anyway.

nit: I think you can combine this check/loop and have the error message just be "Transaction output amount must be valid". this avoids two loops and overly strict hand holding for errors that should never happen anyway.
Bruce039 (Migrated from github.com) reviewed 2026-09-29 13:46:47 +00:00
Bruce039 (Migrated from github.com) commented 2026-09-29 13:46:47 +00:00

Done: the negative-amount check is folded into the MoneyRange loop, with the single "Transaction output amount must be valid" error. (Changes prepared with AI tools.)

Done: the negative-amount check is folded into the MoneyRange loop, with the single "Transaction output amount must be valid" error. (Changes prepared with AI tools.)
maflcko (Migrated from github.com) reviewed 2026-09-29 13:51:26 +00:00
maflcko (Migrated from github.com) commented 2026-09-29 13:51:26 +00:00
Please squash your commits according to https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits
furszy (Migrated from github.com) reviewed 2026-09-29 14:03:50 +00:00
furszy (Migrated from github.com) left a comment

Can move the check below the existing loop:

github.com/bitcoin/bitcoin@dc2a9987aa/src/wallet/spend.cpp (L1092-L1097)

and could also merge the negative amount checks there too. So it is just one loop.

Can move the check below the existing loop: https://github.com/bitcoin/bitcoin/blob/dc2a9987aab711abdc1a77aa6d1462c6a46deabd/src/wallet/spend.cpp#L1092-L1097 and could also merge the negative amount checks there too. So it is just one loop.
Bruce039 commented 2026-09-29 14:17:25 +00:00 (Migrated from github.com)

Squashed into one commit (bab579e56a). Following furszy, the MoneyRange check now sits in the existing recipients loop in CreateTransactionInternal, and it also covers the negative-amount case, so the separate check in CreateTransaction is gone. Added a negative output to the test. (Changes prepared with AI tools.)

Squashed into one commit (bab579e56a). Following furszy, the MoneyRange check now sits in the existing recipients loop in `CreateTransactionInternal`, and it also covers the negative-amount case, so the separate check in `CreateTransaction` is gone. Added a negative output to the test. (Changes prepared with AI tools.)
sedited commented 2026-11-06 20:44:50 +00:00 (Migrated from github.com)

I used AI, but only for the PR description

Please write your own pull request descriptions. I find yours currently hard to read. Putting things into your own words focusing on why this change is being made makes it easier for reviewers.

> I used AI, but only for the PR description Please write your own pull request descriptions. I find yours currently hard to read. Putting things into your own words focusing on _why_ this change is being made makes it easier for reviewers.
maflcko commented 2026-11-07 10:27:37 +00:00 (Migrated from github.com)

lgtm ACK bab579e56a

Seems fine as a style cleanup with test coverage and to make sanitizers happy while fuzzing (not sure if anyone is doing this)

lgtm ACK bab579e56aa8b80a6f2923285e55cbcb2a7730b8 Seems fine as a style cleanup with test coverage and to make sanitizers happy while fuzzing (not sure if anyone is doing this)
Zippo76 (Migrated from github.com) reviewed 2026-11-07 22:17:55 +00:00
Zippo76 (Migrated from github.com) left a comment

tACK bab579e56a

Tested on macOS 27.0.1 arm64, comparing against master 49c3c7fe09. The PR’s wallet_fundrawtransaction.py fails on master with Insufficient funds instead of Transaction output amount must be valid, and passes on the PR.

I also checked these cases manually on regtest:

Output amounts                 Master                                     PR
-1 sat                         Transaction amounts must not be negative   Transaction output amount must be valid
MAX_MONEY + 1 sat              Insufficient funds                         Transaction output amount must be valid
MAX_MONEY and 1 sat            Transaction amount too small               Transaction output amount must be valid
Two outputs of 2^62 sat each   Node aborts                                Transaction output amount must be valid

The abort on master reports:

Assertion failed: (!m_selected_inputs.empty()), function RecalculateWaste, file coinselection.cpp, line 904.

With the PR, all four calls return RPC error -4 and the node remains running.

tACK bab579e56a Tested on macOS 27.0.1 arm64, comparing against master `49c3c7fe09`. The PR’s `wallet_fundrawtransaction.py` fails on master with `Insufficient funds` instead of `Transaction output amount must be valid`, and passes on the PR. I also checked these cases manually on regtest: ```text Output amounts Master PR -1 sat Transaction amounts must not be negative Transaction output amount must be valid MAX_MONEY + 1 sat Insufficient funds Transaction output amount must be valid MAX_MONEY and 1 sat Transaction amount too small Transaction output amount must be valid Two outputs of 2^62 sat each Node aborts Transaction output amount must be valid ``` The abort on master reports: ```text Assertion failed: (!m_selected_inputs.empty()), function RecalculateWaste, file coinselection.cpp, line 904. ``` With the PR, all four calls return RPC error -4 and the node remains running.
furszy (Migrated from github.com) reviewed 2026-11-09 21:53:36 +00:00
furszy (Migrated from github.com) left a comment

utACK bab579e56a

utACK bab579e56aa8b80a6f2923285e55cbcb2a7730b8
Lines 1103-1112
furszy (Migrated from github.com) commented 2026-11-09 21:51:48 +00:00

nano nit: could move the !MoneyRange(recipients_sum + recipient.nAmount) here so it just checks for !MoneyRange(recipients_sum) instead.

nano nit: could move the `!MoneyRange(recipients_sum + recipient.nAmount)` here so it just checks for `!MoneyRange(recipients_sum)` instead.
maflcko (Migrated from github.com) reviewed 2026-11-10 06:58:35 +00:00
Lines 1103-1112
maflcko (Migrated from github.com) commented 2026-11-10 06:58:35 +00:00

I think it is nice to have a single check and error for this kind, so I'd prefer to leave as-is

I think it is nice to have a single check and error for this kind, so I'd prefer to leave as-is
maflcko commented 2026-11-17 09:04:00 +00:00 (Migrated from github.com)

I used AI, but only for the PR description

Please write your own pull request descriptions.

@Bruce039 That is the part where AI is not allowed to be used, per the policy. Can you please write the description briefly in your own words? No one wants to read stuff like "the tests passed", etc. The CI runs the tests and any reviewer is welcome to test themselves.

> > I used AI, but only for the PR description > > Please write your own pull request descriptions. @Bruce039 That is the part where AI is not allowed to be used, per the policy. Can you please write the description briefly in your own words? No one wants to read stuff like "the tests passed", etc. The CI runs the tests and any reviewer is welcome to test themselves.
achow101 (Migrated from github.com) reviewed 2026-11-20 23:18:06 +00:00
achow101 (Migrated from github.com) left a comment
ACK bab579e56aa8b80a6f2923285e55cbcb2a7730b8
achow101 (Migrated from github.com) commented 2026-11-20 23:18:00 +00:00

bytearray is unnecessary

`bytearray` is unnecessary
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36342
No description provided.