What changed, and why it matters
This patch fixes a bug in Monero's wallet software that could misreport the amount of money involved when a user spent funds from one account and received change in a different account. Previously, the wallet only tracked a single total for received amounts, which could lead to incorrect balance or transaction history display when change went to a different subaddress account. The fix tracks received amounts separately per account and uses only the change belonging to the spending account when recording the outgoing transaction. A new functional test verifies this scenario.
Review and merge the patch. Users relying on view-only or cold wallets with multiple subaddress accounts should update and rescan/re-import key images if they have performed cross-account self-transfers, to ensure balances and transaction histories are accurate.
Security signals we found
Incorrect aggregation of change outputs across subaddress accounts in import_key_images
Potential balance or transaction-history misreporting in view-only / cold-signing wallets
Fix scoped to per-account received amount tracking
New functional test covering cross-account self-transfer
Evidence from the diff
In wallet2::import_key_images(), the code previously aggregated all received outputs (including change) into a single uint64_t tx_money_got_in_outs. When a transaction spent inputs from one subaddress account but change was returned to a different account, the outgoing payment record created by process_outgoing() would include change from the wrong account, distorting the per-account balance/history. The patch replaces the scalar with an unordered_map
Changed components
src/wallet/wallet2.cppwallet2::import_key_images()process_outgoing()tests/functional_tests/cold_signing.pyInspect captured patch +39 / −5
### src/wallet/wallet2.cpp
@@ -13817,8 +13817,8 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
THROW_WALLET_EXCEPTION_IF(!(spnet_txid_parsed == *it), error::wallet_internal_error, "parsed txid mismatch");
++it;
- // get received (change) amount
- uint64_t tx_money_got_in_outs = 0;
+ // get received amounts by subaddress account
+ std::unordered_map<uint32_t, uint64_t> received_by_account;
const cryptonote::account_keys& keys = m_account.get_keys();
const crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(spent_tx);
crypto::key_derivation derivation;
@@ -13846,9 +13846,10 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
rct::key mask;
tx_scan_info.money_transfered = tools::decodeRct(spent_tx.rct_signatures, tx_scan_info.received->derivation, output_index, mask, hwdev);
}
- THROW_WALLET_EXCEPTION_IF(tx_money_got_in_outs >= std::numeric_limits<uint64_t>::max() - tx_scan_info.money_transfered,
+ uint64_t &account_received = received_by_account[tx_scan_info.received->index.major];
+ THROW_WALLET_EXCEPTION_IF(account_received >= std::numeric_limits<uint64_t>::max() - tx_scan_info.money_transfered,
error::wallet_internal_error, "Overflow in received amounts");
- tx_money_got_in_outs += tx_scan_info.money_transfered;
+ account_received += tx_scan_info.money_transfered;
}
++output_index;
}
@@ -13888,7 +13889,7 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
}
// create outgoing payment
- process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, tx_money_got_in_outs, subaddr_account, subaddr_indices);
+ process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, received_by_account[subaddr_account], subaddr_account, subaddr_indices);
// erase corresponding incoming payment
for (auto j = m_payments.begin(); j != m_payments.end(); ++j)
### tests/functional_tests/cold_signing.py
@@ -51,6 +51,7 @@ def run_test(self):
self.self_transfer_to_subaddress(piecemeal_output_export)
self.transfer_after_empty_export_import()
self.transfer_to_multi_subaddresses()
+ self.self_transfer_to_other_account()
self.sweep()
def reset(self):
@@ -294,6 +295,7 @@ def in_hash_list(txid, tx_hash_list):
return len([x for x in tx_hash_list if x == txid]) > 0
assert len([x for x in (res['pending'] if 'pending' in res else []) if in_hash_list(x.txid, tx_hash_list)]) == 0
assert len([x for x in (res['out'] if 'out' in res else []) if in_hash_list(x.txid, tx_hash_list)]) > 0
+ return tx_hash_list
def transfer_to_multi_subaddresses(self):
# This test triggers the non-standard additional keys case
@@ -307,6 +309,37 @@ def transfer_to_multi_subaddresses(self):
self.sign_and_submit(res.unsigned_txset)
+ def self_transfer_to_other_account(self):
+ print("Self-spending to another account in a view-only wallet")
+ self.export_import(False)
+ destination = self.hot_wallet.create_account().address
+ assert self.cold_wallet.create_account().address == destination
+
+ observer = Wallet(idx = 1)
+ try: observer.close_wallet()
+ except: pass
+ observer.generate_from_keys(viewkey = self.cold_wallet.query_key("view_key").key, address = STANDARD_ADDRESS)
+ assert observer.create_account().address == destination
+ observer.refresh()
+
+ amount = 1000000000000
+ res = self.hot_wallet.transfer([{'address': destination, 'amount': amount}])
+ txids = self.sign_and_submit(res.unsigned_txset)
+ assert len(txids) == 1
+ txid = txids[0]
+
+ observer.refresh()
+ outputs = observer.export_outputs()
+ self.cold_wallet.import_outputs(outputs.outputs_data_hex)
+ key_images = self.cold_wallet.export_key_images(True)
+ observer.import_key_images(key_images.signed_key_images, offset = key_images.offset)
+
+ expected = [x for x in self.hot_wallet.get_transfers(all_accounts = True)['out'] if x.txid == txid]
+ actual = [x for x in observer.get_transfers(all_accounts = True)['out'] if x.txid == txid]
+ assert len(expected) == len(actual) == 1
+ assert expected[0].amount == amount
+ assert actual[0].amount == expected[0].amount, (actual[0].amount, expected[0].amount)
+
def sweep(self):
print("Mine 10 blocks so all non-coinbase outs from prior txs unlock")
self.mine(10)Why this scored 57/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.