What changed, and why it matters
This change fixes how a Monero cold wallet handles imported output files that are empty or in an older format. Previously, the wallet could misread an empty export and either fail to clear stale outputs or incorrectly treat the old format as valid, which could lead to a cold wallet showing wrong balances or accepting incomplete state. The patch makes the logic clearer: try the new format first, and only if that fails try the old format. New tests confirm that after a blockchain reorganization an empty export correctly clears outputs, and that an empty incremental export cannot be used to rebuild a cold wallet from scratch.
Review the new import_outputs_from_str control flow to confirm it rejects malformed/empty inputs safely, and run the updated cold_signing functional tests. Consider whether additional negative tests are needed for adversarially crafted output exports.
Security signals we found
Logic error in output import path could cause stale outputs to persist or empty exports to be mishandled
Cold wallet state synchronization issue after blockchain reorganization
New functional tests assert correct behavior for empty and incremental output exports
Evidence from the diff
In wallet2::import_outputs_from_str, the original code parsed a JSON archive into new_outputs, cleared its transfer_details vector on failure, then parsed a binary archive into a separate outputs tuple, zeroed that tuple on failure, and finally chose between them using a nested ternary based on whether the vectors were non-empty. This logic was fragile: an empty but valid new-format export would have an empty vector and fall through to the old-format path, and failures/zeroing were conflated. The patch restructures the flow so that if the new-format deserialization succeeds, import_outputs(new_outputs) is called immediately; otherwise it attempts the old format and calls import_outputs(outputs) only if that succeeds. The functional tests add two scenarios: (1) a reorg replaces the wallet’s mined output with nothing, and exporting/importing an empty output set correctly leaves the cold wallet empty; (2) a recreated cold wallet cannot be recovered from an empty incremental export alone and still requires a full export.
Changed components
src/wallet/wallet2.cpptests/functional_tests/cold_signing.pyInspect captured patch +63 / −18
### src/wallet/wallet2.cpp
@@ -14903,27 +14903,23 @@ size_t wallet2::import_outputs_from_str(const std::string &outputs_st)
loaded = true;
}
catch (...) {}
- if (!loaded)
- std::get<2>(new_outputs).clear();
-
- std::tuple<uint64_t, uint64_t, std::vector<tools::wallet2::transfer_details>> outputs;
- if (!loaded) try
+ if (loaded)
+ imported_outputs = import_outputs(new_outputs);
+ else
{
- binary_archive<false> ar{epee::strspan<std::uint8_t>(body)};
- if (::serialization::serialize(ar, outputs))
- if (::serialization::check_stream_state(ar))
- loaded = true;
- }
- catch (...) {}
+ std::tuple<uint64_t, uint64_t, std::vector<tools::wallet2::transfer_details>> outputs;
+ try
+ {
+ binary_archive<false> ar{epee::strspan<std::uint8_t>(body)};
+ if (::serialization::serialize(ar, outputs))
+ if (::serialization::check_stream_state(ar))
+ loaded = true;
+ }
+ catch (...) {}
- if (!loaded)
- {
- std::get<0>(outputs) = 0;
- std::get<1>(outputs) = 0;
- std::get<2>(outputs) = {};
+ if (loaded)
+ imported_outputs = import_outputs(outputs);
}
-
- imported_outputs = !std::get<2>(new_outputs).empty() ? import_outputs(new_outputs) : !std::get<2>(outputs).empty() ? import_outputs(outputs) : 0;
}
catch (const std::exception &e)
{
### tests/functional_tests/cold_signing.py
@@ -37,14 +37,17 @@
SEED = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted'
STANDARD_ADDRESS = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
+OTHER_ADDRESS = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW'
SUBADDRESS = '84QRUYawRNrU3NN1VpFRndSukeyEb3Xpv8qZjjsoJZnTYpDYceuUTpog13D7qPxpviS7J29bSgSkR11hFFoXWk2yNdsR9WF'
SUBADDRESS2 = '85M4M1RVRcoEeC8sdSxN1ef6GhQYChSfKPWkB4FLKYJiSWuMXXT4Ewv8BHCRzSJB4ZYvXUcFxN4DPcVu6uwoPNRvQ1QwaXB'
class ColdSigningTest():
def run_test(self):
self.reset()
self.create(0)
+ self.empty_export_after_reorg()
self.mine()
+ self.recover_from_empty_incremental_export()
for piecemeal_output_export in [False, True]:
self.transfer(piecemeal_output_export)
for piecemeal_output_export in [False, True]:
@@ -102,6 +105,52 @@ def mine(self, n_blocks = 80):
daemon.generateblocks(STANDARD_ADDRESS, n_blocks)
wallet.refresh()
+ def empty_export_after_reorg(self):
+ print("Importing an empty output export after a reorg")
+ daemon = Daemon()
+ daemon.generateblocks(STANDARD_ADDRESS, 1)
+ self.hot_wallet.refresh()
+ assert len(self.hot_wallet.incoming_transfers().get('transfers', [])) == 1
+
+ outputs = self.hot_wallet.export_outputs()
+ self.cold_wallet.import_outputs(outputs.outputs_data_hex)
+ assert len(self.cold_wallet.incoming_transfers().get('transfers', [])) == 1
+
+ daemon.pop_blocks(1)
+ daemon.generateblocks(OTHER_ADDRESS, 1)
+ self.hot_wallet.refresh()
+ assert not self.hot_wallet.incoming_transfers().get('transfers', [])
+
+ outputs = self.hot_wallet.export_outputs()
+ self.cold_wallet.import_outputs(outputs.outputs_data_hex)
+ assert not self.cold_wallet.incoming_transfers().get('transfers', [])
+
+ def recover_from_empty_incremental_export(self):
+ print("Recovering a recreated cold wallet from an empty incremental export")
+ self.hot_wallet.refresh()
+ full_export = self.hot_wallet.export_outputs(all = True)
+ self.cold_wallet.import_outputs(full_export.outputs_data_hex)
+ key_images = self.cold_wallet.export_key_images(True)
+ self.hot_wallet.import_key_images(key_images.signed_key_images, offset = key_images.offset)
+
+ incremental_export = self.hot_wallet.export_outputs()
+ self.cold_wallet.close_wallet()
+ self.cold_wallet.restore_deterministic_wallet(seed = SEED)
+ assert not self.cold_wallet.incoming_transfers().get('transfers', [])
+
+ try:
+ self.cold_wallet.import_outputs(incremental_export.outputs_data_hex)
+ except Exception as e:
+ assert 'Imported outputs omit more outputs that we know of' in str(e), str(e)
+ else:
+ assert False, "empty incremental export should not restore a cold wallet"
+ assert not self.cold_wallet.incoming_transfers().get('transfers', [])
+
+ self.cold_wallet.import_outputs(full_export.outputs_data_hex)
+ assert len(self.cold_wallet.incoming_transfers().get('transfers', [])) == len(self.hot_wallet.incoming_transfers().get('transfers', []))
+ tx = self.hot_wallet.transfer([{'address': STANDARD_ADDRESS, 'amount': 1000000000000}])
+ self.sign_and_submit(tx.unsigned_txset)
+
def export_import(self, piecemeal_output_export):
self.hot_wallet.refresh()
Why this scored 44/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.