Merge bitcoin/bitcoin#36339: wallet: avoid int overflow in listtransactions
What changed, and why it matters
This patch fixes a bug in Bitcoin Core's `listtransactions` RPC command. When a user asked for a very large number of transactions plus a large skip value, the wallet code added two integers together and overflowed, producing a negative number. That negative number was then used as a memory range, causing the node to crash. The fix reads the values as unsigned 32-bit integers and adds them in 64-bit arithmetic so the sum cannot overflow, and removes the now-redundant manual negative checks.
Apply the patch. Ensure RPC wallet endpoints consistently use unsigned or widened arithmetic for user-supplied counts and offsets, and consider adding regression tests for other pagination-style RPCs.
Security signals we found
signed integer overflow in RPC input handling
out-of-bounds iterator range leading to memory access violation
node crash (denial of service) from authenticated RPC call
ASan/UBSan-reported undefined behavior confirmed by reporter
missing input bounds clamping before arithmetic
Evidence from the diff
The listtransactions RPC previously parsed count and skip (nFrom) as int, rejected negatives manually, then computed nCount + nFrom in signed 32-bit arithmetic. A request such as count=2147483647, skip=1 overflows INT_MAX, yielding a negative sum. The loop termination check then becomes true immediately, and the subsequent clamping arithmetic also overflows, leaving nCount corrupted. The final UniValue::push_backV call uses an iterator range computed from the negative value, resulting in an out-of-bounds memory access and node crash (ASan/UBSan confirmed). The patch changes parsing to uint32_t via self.Arg, drops the manual negative checks, and stores the sum in uint64_t. Slicing is then done with size_t and std::min, eliminating the overflow and unsafe iterator arithmetic.
Changed components
src/wallet/rpc/transactions.cppwallet listtransactions RPCtest/functional/wallet_listtransactions.pyInspect captured patch +22 / −20
### src/wallet/rpc/transactions.cpp
@@ -558,43 +558,34 @@ RPCMethod listtransactions()
throw JSONRPCError(RPC_INVALID_PARAMETER, "Label argument must be a valid label name or \"*\".");
}
}
- int nCount = 10;
- if (!request.params[1].isNull())
- nCount = request.params[1].getInt<int>();
- int nFrom = 0;
- if (!request.params[2].isNull())
- nFrom = request.params[2].getInt<int>();
-
- if (nCount < 0)
- throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
- if (nFrom < 0)
- throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
+ const auto count{self.Arg<uint32_t>("count")};
+ const auto skip{self.Arg<uint32_t>("skip")};
+ // Both are at most UINT32_MAX, so the sum can't overflow in 64 bits
+ const uint64_t limit{uint64_t{count} + skip};
std::vector<UniValue> ret;
{
LOCK(pwallet->cs_wallet);
const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
- // iterate backwards until we have nCount items to return:
+ // iterate backwards until we have count items to return:
for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
{
CWalletTx *const pwtx = (*it).second;
ListTransactions(*pwallet, *pwtx, 0, true, ret, filter_label);
- if ((int)ret.size() >= (nCount+nFrom)) break;
+ if (ret.size() >= limit) break;
}
}
// ret is newest to oldest
- if (nFrom > (int)ret.size())
- nFrom = ret.size();
- if ((nFrom + nCount) > (int)ret.size())
- nCount = ret.size() - nFrom;
+ const size_t from{std::min<size_t>(skip, ret.size())};
+ const size_t num{std::min<size_t>(count, ret.size() - from)};
auto txs_rev_it{std::make_move_iterator(ret.rend())};
UniValue result{UniValue::VARR};
- result.push_backV(txs_rev_it - nFrom - nCount, txs_rev_it - nFrom); // Return oldest to newest
+ result.push_backV(txs_rev_it - from - num, txs_rev_it - from); // Return oldest to newest
return result;
},
};
### test/functional/wallet_listtransactions.py
@@ -187,8 +187,19 @@ def run_invalid_parameters_test(self):
self.log.info("Test listtransactions RPC parameter validity")
assert_raises_rpc_error(-8, 'Label argument must be a valid label name or "*".', self.nodes[0].listtransactions, label="")
self.nodes[0].listtransactions(label="*")
- assert_raises_rpc_error(-8, "Negative count", self.nodes[0].listtransactions, count=-1)
- assert_raises_rpc_error(-8, "Negative from", self.nodes[0].listtransactions, skip=-1)
+ assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].listtransactions, count=-1)
+ assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].listtransactions, skip=-1)
+
+ self.log.info("Test listtransactions with large count and skip")
+ INT_MAX = 2**31 - 1
+ UINT32_MAX = 2**32 - 1
+ all_but_newest = self.nodes[0].listtransactions(count=1000000, skip=1)
+ assert_equal(self.nodes[0].listtransactions(count=INT_MAX, skip=1), all_but_newest)
+ assert_equal(self.nodes[0].listtransactions(count=UINT32_MAX, skip=1), all_but_newest)
+ assert_equal(self.nodes[0].listtransactions(count=INT_MAX, skip=INT_MAX), [])
+ assert_equal(self.nodes[0].listtransactions(count=1, skip=INT_MAX), [])
+ assert_equal(self.nodes[0].listtransactions(count=UINT32_MAX, skip=UINT32_MAX), [])
+ assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].listtransactions, count=UINT32_MAX + 1)
def test_op_return(self):
"""Test if OP_RETURN outputs will be displayed correctly."""Why this scored 74/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.