What changed, and why it matters
This Monero wallet patch changes how watch-only wallet exports are created. Previously, the exported watch-only file was encrypted with the same password as the original full wallet. Now users can set a separate password for the watch-only export. More importantly, the patch also strips sensitive material out of that export: it removes the Monero-C passphrase and disables/removes custom background-sync keys from the watch-only file. The code also verifies the original wallet password before creating the export. The change appears to be a security hardening fix for information leakage when exporting a watch-only wallet.
Treat this as a security hardening patch and include it in the next release. Users who previously exported watch-only wallets should be advised to re-export them with the updated software if they used Monero-C passphrases or custom background-sync passwords, because older exports may have included those secrets encrypted only under the original wallet password.
Security signals we found
Sensitive data removal from exported watch-only wallet (Monero-C passphrase and custom background-sync key)
Addition of password verification before watch-only export
Ability to use a distinct password for watch-only export, reducing key-derivation reuse
Unit tests explicitly assert that custom_background_key is absent from watch-only export and that wrong original password throws invalid_password
Evidence from the diff
The commit refactors wallet2::write_watch_only_wallet() to accept two passwords: original_password (to unlock/decrypt the source wallet) and watch_only_password (to encrypt the new keys file). It adds a new get_keys_file_data() overload that takes both the new encryption key and the original key, plus a sanitize_watch_only_export flag. When that flag is true, account.forget_monero_c_passphrase() is called and background_sync_type is forced to BackgroundSyncOff, and the custom_background_key JSON field is omitted. A new forget_monero_c_passphrase() helper securely wipes m_keys.m_monero_c_passphrase. simplewallet now prompts for the current wallet password before prompting for the new watch-only password, and verifies it via verify_password_with_cached_key(). Unit tests confirm that the watch-only file can be loaded only with the new password, that the Monero-C passphrase is absent, and that custom background keys are not included.
Changed components
src/wallet/wallet2.cppsrc/wallet/wallet2.hsrc/simplewallet/simplewallet.cppsrc/cryptonote_basic/account.cppsrc/cryptonote_basic/account.htests/unit_tests/wallet_storage.cppInspect captured patch +215 / −7
### src/cryptonote_basic/account.cpp
@@ -160,6 +160,14 @@ DISABLE_VS_WARNINGS(4244 4345)
m_keys.m_polyseed = crypto::secret_key();
}
//-----------------------------------------------------------------
+ void account_base::forget_monero_c_passphrase()
+ {
+ auto &passphrase = m_keys.m_monero_c_passphrase.buffer;
+ if (!passphrase.empty())
+ memwipe(passphrase.data(), passphrase.size());
+ passphrase.clear();
+ }
+ //-----------------------------------------------------------------
void account_base::set_spend_key(const crypto::secret_key& spend_secret_key)
{
// make sure derived spend public key matches saved public spend key
### src/cryptonote_basic/account.h
@@ -136,6 +136,7 @@ namespace cryptonote
void set_createtime(uint64_t val) { m_creation_timestamp = val; }
void forget_spend_key();
+ void forget_monero_c_passphrase();
void set_spend_key(const crypto::secret_key& spend_secret_key);
void set_polyseed(const crypto::secret_key& polyseed);
const std::vector<crypto::secret_key> &get_multisig_keys() const { return m_keys.m_multisig_keys; }
### src/simplewallet/simplewallet.cpp
@@ -5246,6 +5246,10 @@ bool simple_wallet::save_watch_only(const std::vector<std::string> &args/* = std
return true;
}
+ const auto orig_pwd_container = get_and_verify_password();
+ if (!orig_pwd_container)
+ return true;
+
const auto pwd_container = password_prompter(tr("Password for new watch-only wallet"), true);
if (!pwd_container)
@@ -5257,7 +5261,7 @@ bool simple_wallet::save_watch_only(const std::vector<std::string> &args/* = std
try
{
std::string new_keys_filename;
- m_wallet->write_watch_only_wallet(m_wallet_file, pwd_container->password(), new_keys_filename);
+ m_wallet->write_watch_only_wallet(m_wallet_file, orig_pwd_container->password(), pwd_container->password(), new_keys_filename);
success_msg_writer() << tr("Watch only wallet saved as: ") << new_keys_filename;
}
catch (const std::exception &e)
### src/wallet/wallet2.cpp
@@ -4806,6 +4806,11 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const epee:
}
//----------------------------------------------------------------------------------------------------
boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const crypto::chacha_key& key, bool watch_only, bool background_keys_file)
+{
+ return get_keys_file_data(key, key, watch_only, background_keys_file, false);
+}
+//----------------------------------------------------------------------------------------------------
+boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const crypto::chacha_key& key, const crypto::chacha_key& original_key, bool watch_only, bool background_keys_file, bool sanitize_watch_only_export)
{
epee::byte_slice account_data;
std::string multisig_signers;
@@ -4814,12 +4819,14 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const crypt
if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
{
- account.encrypt_viewkey(key);
- account.decrypt_keys(key);
+ account.encrypt_viewkey(original_key);
+ account.decrypt_keys(original_key);
}
if (watch_only || background_keys_file)
account.forget_spend_key();
+ if (sanitize_watch_only_export)
+ account.forget_monero_c_passphrase();
account.encrypt_keys(key);
@@ -4953,7 +4960,7 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const crypt
value2.SetInt(m_track_uses ? 1 : 0);
json.AddMember("track_uses", value2, json.GetAllocator());
- value2.SetInt(m_background_sync_type);
+ value2.SetInt(sanitize_watch_only_export ? BackgroundSyncOff : m_background_sync_type);
json.AddMember("background_sync_type", value2, json.GetAllocator());
value2.SetInt(m_show_wallet_name_when_locked ? 1 : 0);
@@ -5019,7 +5026,7 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const crypt
value2.SetInt(m_polyseed ? 1 : 0);
json.AddMember("polyseed", value2, json.GetAllocator());
- if (m_background_sync_type == BackgroundSyncCustomPassword && !background_keys_file && m_custom_background_key)
+ if (!sanitize_watch_only_export && m_background_sync_type == BackgroundSyncCustomPassword && !background_keys_file && m_custom_background_key)
{
value.SetString(reinterpret_cast<const char*>(m_custom_background_key.get().data()), m_custom_background_key.get().size());
json.AddMember("custom_background_key", value, json.GetAllocator());
@@ -6523,17 +6530,36 @@ void wallet2::rewrite(const std::string& wallet_name, const epee::wipeable_strin
/*!
* \brief Writes to a file named based on the normal wallet (doesn't generate key, assumes it's already there)
* \param wallet_name Base name of wallet file
- * \param password Password for wallet file
+ * \param password Password for current wallet file, reused for the new watch-only wallet
* \param new_keys_filename [OUT] Name of new keys file
*/
void wallet2::write_watch_only_wallet(const std::string& wallet_name, const epee::wipeable_string& password, std::string &new_keys_filename)
{
+ write_watch_only_wallet(wallet_name, password, password, new_keys_filename);
+}
+//----------------------------------------------------------------------------------------------------
+/*!
+ * \brief Writes a watch-only keys file encrypted with its own password
+ * \param wallet_name Base name of wallet file
+ * \param original_password Password for the current wallet
+ * \param watch_only_password Password for the new watch-only wallet
+ * \param new_keys_filename [OUT] Name of new keys file
+ */
+void wallet2::write_watch_only_wallet(const std::string& wallet_name, const epee::wipeable_string& original_password, const epee::wipeable_string& watch_only_password, std::string &new_keys_filename)
+{
+ crypto::chacha_key original_key, watch_only_key;
+ crypto::generate_chacha_key(original_password.data(), original_password.size(), original_key, m_kdf_rounds);
+ verify_password_with_cached_key(original_key);
+ crypto::generate_chacha_key(watch_only_password.data(), watch_only_password.size(), watch_only_key, m_kdf_rounds);
+
prepare_file_names(wallet_name);
boost::system::error_code ignored_ec;
new_keys_filename = m_wallet_file + "-watchonly.keys";
bool watch_only_keys_file_exists = boost::filesystem::exists(new_keys_filename, ignored_ec);
THROW_WALLET_EXCEPTION_IF(watch_only_keys_file_exists, error::file_save_error, new_keys_filename);
- bool r = store_keys(new_keys_filename, password, true);
+ boost::optional<wallet2::keys_file_data> keys_file_data = get_keys_file_data(watch_only_key, original_key, true, false, true);
+ THROW_WALLET_EXCEPTION_IF(!keys_file_data, error::file_save_error, new_keys_filename);
+ bool r = store_keys_file_data(new_keys_filename, keys_file_data.get());
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, new_keys_filename);
}
//----------------------------------------------------------------------------------------------------
### src/wallet/wallet2.h
@@ -760,6 +760,7 @@ namespace tools
*/
void rewrite(const std::string& wallet_name, const epee::wipeable_string& password);
void write_watch_only_wallet(const std::string& wallet_name, const epee::wipeable_string& password, std::string &new_keys_filename);
+ void write_watch_only_wallet(const std::string& wallet_name, const epee::wipeable_string& original_password, const epee::wipeable_string& watch_only_password, std::string &new_keys_filename);
void load(const std::string& wallet, const epee::wipeable_string& password, const std::string& keys_buf = "", const std::string& cache_buf = "");
void store();
/*!
@@ -1542,6 +1543,7 @@ namespace tools
bool store_keys(const std::string& keys_file_name, const epee::wipeable_string& password, bool watch_only = false);
bool store_keys(const std::string& keys_file_name, const crypto::chacha_key& key, bool watch_only = false, bool background_keys_file = false);
boost::optional<wallet2::keys_file_data> get_keys_file_data(const crypto::chacha_key& key, bool watch_only = false, bool background_keys_file = false);
+ boost::optional<wallet2::keys_file_data> get_keys_file_data(const crypto::chacha_key& key, const crypto::chacha_key& original_key, bool watch_only, bool background_keys_file, bool sanitize_watch_only_export);
bool store_keys_file_data(const std::string& keys_file_name, wallet2::keys_file_data &keys_file_data, bool background_keys_file = false);
/*!
* \brief Load wallet keys information from wallet file.
### tests/unit_tests/wallet_storage.cpp
@@ -47,6 +47,34 @@ static constexpr const char WALLET2_ASCII_OUTPUT_MAGIC[] = "MoneroAsciiDataV1";
class wallet_accessor_test
{
public:
+ static void set_monero_c_passphrase(tools::wallet2 &wallet, const std::string &passphrase)
+ {
+ auto &keys = const_cast<cryptonote::account_keys&>(wallet.get_account().get_keys());
+ keys.m_monero_c_passphrase.buffer.assign(passphrase.begin(), passphrase.end());
+ }
+
+ static bool has_custom_background_key(const tools::wallet2 &wallet)
+ {
+ return !!wallet.m_custom_background_key;
+ }
+
+ static bool watch_only_keys_contain_custom_background_key(tools::wallet2 &wallet,
+ const epee::wipeable_string &original_password, const epee::wipeable_string &watch_only_password)
+ {
+ crypto::chacha_key original_key, watch_only_key;
+ wallet.generate_chacha_key_from_password(original_password, original_key);
+ wallet.generate_chacha_key_from_password(watch_only_password, watch_only_key);
+ const auto keys_file_data = wallet.get_keys_file_data(watch_only_key, original_key, true, false, true);
+ if (!keys_file_data)
+ return false;
+ std::string json(keys_file_data->account_data.size(), '\0');
+ crypto::chacha20(keys_file_data->account_data.data(), keys_file_data->account_data.size(),
+ watch_only_key, keys_file_data->iv, &json[0]);
+ const bool contains_key = json.find("\"custom_background_key\"") != std::string::npos;
+ memwipe(&json[0], json.size());
+ return contains_key;
+ }
+
static void forget_cached_key_image(tools::wallet2 &wallet, const size_t index)
{
crypto::key_image stale_key_image = AUTO_VAL_INIT(stale_key_image);
@@ -223,6 +251,145 @@ TEST(wallet_storage, change_password_same_file)
}
}
+TEST(wallet_storage, watch_only_wallet_with_distinct_password)
+{
+ const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a";
+ const path wallet_file = unit_test::data_dir / "wallet_watch_only_distinct_password";
+ const std::string watch_only_file = wallet_file.string() + "-watchonly";
+ const epee::wipeable_string original_password("beepbeep");
+ const epee::wipeable_string watch_only_password("different password");
+ const epee::wipeable_string wrong_password("wrong password");
+ const std::string source_passphrase("source wallet passphrase");
+
+ ASSERT_TRUE(is_file_exist(source_wallet_file.string()));
+ ASSERT_TRUE(is_file_exist(source_wallet_file.string() + ".keys"));
+ remove(wallet_file);
+ remove(wallet_file.string() + ".keys");
+ remove(watch_only_file);
+ remove(watch_only_file + ".keys");
+ tools::copy_file(source_wallet_file.string(), wallet_file.string());
+ tools::copy_file(source_wallet_file.string() + ".keys", wallet_file.string() + ".keys");
+
+ std::string new_keys_filename;
+ {
+ tools::wallet2 w;
+ w.load(wallet_file.string(), original_password);
+ ASSERT_EQ(tools::wallet2::AskPasswordToDecrypt, w.ask_password());
+ ASSERT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, w.get_address_as_str());
+ EXPECT_THROW(w.write_watch_only_wallet(wallet_file.string(), wrong_password, watch_only_password, new_keys_filename), tools::error::invalid_password);
+ EXPECT_FALSE(is_file_exist(watch_only_file + ".keys"));
+ {
+ tools::wallet_keys_unlocker unlocker(w, &original_password);
+ wallet_accessor_test::set_monero_c_passphrase(w, source_passphrase);
+ }
+ w.write_watch_only_wallet(wallet_file.string(), original_password, watch_only_password, new_keys_filename);
+ EXPECT_EQ(watch_only_file + ".keys", new_keys_filename);
+ EXPECT_TRUE(is_file_exist(new_keys_filename));
+ {
+ tools::wallet_keys_unlocker unlocker(w, &original_password);
+ const auto &passphrase = w.get_account().get_keys().m_monero_c_passphrase.buffer;
+ EXPECT_EQ(source_passphrase, std::string(passphrase.begin(), passphrase.end()));
+ }
+ }
+
+ {
+ tools::wallet2 w;
+ w.load(watch_only_file, watch_only_password);
+ EXPECT_TRUE(w.watch_only());
+ EXPECT_EQ(crypto::null_skey, w.get_account().get_keys().m_spend_secret_key);
+ EXPECT_TRUE(w.get_account().get_keys().m_monero_c_passphrase.buffer.empty());
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, w.get_address_as_str());
+ }
+ {
+ tools::wallet2 w;
+ EXPECT_ANY_THROW(w.load(watch_only_file, original_password));
+ }
+ {
+ tools::wallet2 w;
+ w.load(wallet_file.string(), original_password);
+ EXPECT_FALSE(w.watch_only());
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, w.get_address_as_str());
+ }
+
+ remove(watch_only_file + ".keys");
+ {
+ tools::wallet2 w;
+ w.load(wallet_file.string(), original_password);
+ w.write_watch_only_wallet(wallet_file.string(), original_password, new_keys_filename);
+ }
+ {
+ tools::wallet2 w;
+ w.load(watch_only_file, original_password);
+ EXPECT_TRUE(w.watch_only());
+ EXPECT_EQ(crypto::null_skey, w.get_account().get_keys().m_spend_secret_key);
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, w.get_address_as_str());
+ }
+ remove(watch_only_file + ".keys");
+ remove(wallet_file.string() + ".keys");
+ remove(wallet_file);
+}
+
+TEST(wallet_storage, watch_only_wallet_drops_custom_background_key)
+{
+ const path wallet_file = unit_test::data_dir / "wallet_watch_only_custom_background";
+ const std::string watch_only_file = wallet_file.string() + "-watchonly";
+ const epee::wipeable_string original_password("source password");
+ const epee::wipeable_string background_password("background password");
+ const epee::wipeable_string watch_only_password("watch-only password");
+ remove(watch_only_file + ".keys");
+
+ std::string new_keys_filename;
+ {
+ tools::wallet2 w;
+ w.generate("", original_password);
+ w.setup_background_sync(tools::wallet2::BackgroundSyncCustomPassword, original_password, background_password);
+ ASSERT_EQ(tools::wallet2::BackgroundSyncCustomPassword, w.background_sync_type());
+ ASSERT_TRUE(wallet_accessor_test::has_custom_background_key(w));
+ EXPECT_FALSE(wallet_accessor_test::watch_only_keys_contain_custom_background_key(w, original_password, watch_only_password));
+ w.write_watch_only_wallet(wallet_file.string(), original_password, watch_only_password, new_keys_filename);
+ EXPECT_EQ(watch_only_file + ".keys", new_keys_filename);
+ EXPECT_EQ(tools::wallet2::BackgroundSyncCustomPassword, w.background_sync_type());
+ EXPECT_TRUE(wallet_accessor_test::has_custom_background_key(w));
+ }
+ {
+ tools::wallet2 w;
+ w.load(watch_only_file, watch_only_password);
+ EXPECT_TRUE(w.watch_only());
+ EXPECT_EQ(tools::wallet2::BackgroundSyncOff, w.background_sync_type());
+ EXPECT_FALSE(wallet_accessor_test::has_custom_background_key(w));
+ }
+ remove(watch_only_file + ".keys");
+}
+
+TEST(wallet_storage, watch_only_rewrite_preserves_monero_c_passphrase)
+{
+ const path wallet_file = unit_test::data_dir / "wallet_watch_only_passphrase_rewrite";
+ const epee::wipeable_string password("wallet password");
+ const std::string passphrase("stored monero_c passphrase");
+ remove(wallet_file);
+ remove(wallet_file.string() + ".keys");
+
+ {
+ tools::wallet2 source;
+ source.generate("", password);
+ tools::wallet2 watch_only;
+ watch_only.generate(wallet_file.string(), password, source.get_address(),
+ source.get_account().get_keys().m_view_secret_key);
+ ASSERT_TRUE(watch_only.watch_only());
+ wallet_accessor_test::set_monero_c_passphrase(watch_only, passphrase);
+ watch_only.rewrite(wallet_file.string(), password);
+ }
+ {
+ tools::wallet2 watch_only;
+ watch_only.load(wallet_file.string(), password);
+ ASSERT_TRUE(watch_only.watch_only());
+ const auto &stored = watch_only.get_account().get_keys().m_monero_c_passphrase.buffer;
+ EXPECT_EQ(passphrase, std::string(stored.begin(), stored.end()));
+ }
+ remove(wallet_file.string() + ".keys");
+ remove(wallet_file);
+}
+
TEST(wallet_storage, change_password_different_file)
{
const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a";Why this scored 59/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.