What changed, and why it matters
This Monero wallet update fixes a file-handling bug in the 'save wallet as' function. Previously, the code compared only the main wallet cache path and used a simple path check that could be fooled by symlinks or filenames ending in '.keys'. The patch now checks both the cache file and the encrypted keys file, detects when the destination overlaps the current wallet via symlinks or tricky paths, and rejects unsafe saves. If exploited, an attacker or malfunctioning script could have overwritten or corrupted wallet files; the patch prevents that.
Treat as a security-hardening fix and include in release notes. Users and downstream wallets should upgrade to avoid potential wallet file corruption or loss from malformed save paths or symlink setups. No immediate emergency response is indicated, but wallet software should not be left unpatched.
Security signals we found
Path overlap / self-overwrite protection for wallet cache and keys files
Symlink and hardlink alias detection using boost::filesystem::equivalent
Rejection of ambiguous paths where .keys file and cache file could collide
Addition of defensive unit tests for edge-case filename and symlink scenarios
Evidence from the diff
The change is in wallet2::store_to. It replaces a single weakly_canonical path comparison with explicit absolute-path checks for both the cache file and the .keys file, uses boost::filesystem::equivalent to detect hard-link/symlink aliasing, and throws wallet_internal_error when destination files overlap the source wallet. It also correctly derives target filenames via do_prepare_file_names before the overlap check, so a save to a path ending in ‘.keys’ is handled safely. New unit tests cover keys-filename saves, cache filenames ending in ‘.keys’, symlink/hardlink overlap rejection, and directory-symlink aliases.
Changed components
src/wallet/wallet2.cpptests/unit_tests/wallet_storage.cppwallet save/store_to functionalityInspect captured patch +266 / −6
### src/wallet/wallet2.cpp
@@ -29,6 +29,7 @@
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <algorithm>
+#include <array>
#include <numeric>
#include <optional>
#include <string>
@@ -7229,13 +7230,46 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
// 4. store new cache file
// 5. remove old cache file
- // Avoid canonicalizing paths when explicitly saving to the current wallet file.
+ std::string target_keys_file = m_keys_file;
+ std::string target_wallet_file = m_wallet_file;
+ std::string target_mms_file = m_mms_file;
+ if (!path.empty())
+ do_prepare_file_names(path, target_keys_file, target_wallet_file, target_mms_file);
+
+ // The cache filename itself can end in ".keys". Keep an explicit save to it in place.
bool same_file = had_old_wallet_files && (path.empty() || path == m_wallet_file);
if (had_old_wallet_files && !same_file)
{
- const std::string canonical_old_path = boost::filesystem::canonical(m_wallet_file).string();
- const std::string canonical_new_path = boost::filesystem::weakly_canonical(path).string();
- same_file = canonical_old_path == canonical_new_path;
+ THROW_WALLET_EXCEPTION_IF(!boost::filesystem::exists(m_wallet_file),
+ error::wallet_internal_error, "Cannot move wallet when cache file is missing");
+ const auto old_cache = boost::filesystem::absolute(m_wallet_file);
+ const auto old_keys = boost::filesystem::absolute(m_keys_file);
+ const auto new_cache = boost::filesystem::absolute(target_wallet_file);
+ const auto new_keys = boost::filesystem::absolute(target_keys_file);
+ boost::system::error_code ec;
+ const auto same_entry = [&ec](const boost::filesystem::path &old_file, const boost::filesystem::path &new_file) {
+ return old_file.filename() == new_file.filename() &&
+ boost::filesystem::weakly_canonical(old_file.parent_path()) ==
+ boost::filesystem::weakly_canonical(new_file.parent_path()) &&
+ boost::filesystem::equivalent(old_file, new_file, ec);
+ };
+ same_file = same_entry(old_cache, new_cache) && same_entry(old_keys, new_keys);
+ if (!same_file)
+ {
+ const std::array<boost::filesystem::path, 2> old_files{{old_cache, old_keys}};
+ const std::array<boost::filesystem::path, 2> new_files{{
+ boost::filesystem::weakly_canonical(new_cache),
+ boost::filesystem::weakly_canonical(new_keys)
+ }};
+ for (const auto &old_file : old_files)
+ {
+ for (const auto &new_file : new_files)
+ {
+ THROW_WALLET_EXCEPTION_IF(boost::filesystem::equivalent(old_file, new_file, ec),
+ error::wallet_internal_error, "Destination wallet files overlap the current wallet");
+ }
+ }
+ }
}
THROW_WALLET_EXCEPTION_IF(m_is_background_wallet && !same_file, error::wallet_internal_error,
@@ -7276,15 +7310,17 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
boost::optional<wallet2::cache_file_data> cache_file_data = get_cache_file_data();
THROW_WALLET_EXCEPTION_IF(cache_file_data == boost::none, error::wallet_internal_error, "failed to generate wallet cache data");
- const std::string new_file = same_file ? m_wallet_file + ".new" : path;
+ const std::string new_file = same_file ? m_wallet_file + ".new" : target_wallet_file;
const std::string old_file = m_wallet_file;
const std::string old_keys_file = m_keys_file;
const std::string old_address_file = m_wallet_file + ".address.txt";
const std::string old_mms_file = m_mms_file;
if (!same_file)
{
- prepare_file_names(path);
+ m_keys_file = std::move(target_keys_file);
+ m_wallet_file = std::move(target_wallet_file);
+ m_mms_file = std::move(target_mms_file);
}
if (!same_file || force_rewrite_keys)
### tests/unit_tests/wallet_storage.cpp
@@ -39,6 +39,7 @@
#include "serialization/binary_utils.h"
#include "wallet/wallet2.h"
#include "common/util.h"
+#include "scope_guard.h"
using namespace boost::filesystem;
using namespace epee::file_io_utils;
@@ -414,6 +415,229 @@ TEST(wallet_storage, store_to_file2file)
files_are_expected();
}
+TEST(wallet_storage, store_to_keys_filename)
+{
+ const path dir = temp_directory_path() / unique_path("wallet-store-keys-%%%%-%%%%");
+ create_directories(dir);
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ const path wallet_file = dir / "wallet";
+ const epee::wipeable_string password("test");
+ std::string address;
+ {
+ tools::wallet2 w(cryptonote::MAINNET);
+ w.generate("", password);
+ address = w.get_address_as_str();
+ w.store_to(wallet_file.string() + ".keys", password);
+ EXPECT_EQ(w.path(), wallet_file.string());
+ ASSERT_TRUE(exists(wallet_file));
+ ASSERT_TRUE(exists(wallet_file.string() + ".keys"));
+ }
+
+ tools::wallet2 loaded(cryptonote::MAINNET);
+ loaded.load(wallet_file.string() + ".keys", password);
+ EXPECT_EQ(loaded.get_address_as_str(), address);
+}
+
+TEST(wallet_storage, store_to_current_keys_filename)
+{
+ const path dir = temp_directory_path() / unique_path("wallet-store-current-keys-%%%%-%%%%");
+ create_directories(dir);
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ const path wallet_file = dir / "wallet";
+ const epee::wipeable_string password("test");
+ std::string address;
+ {
+ tools::wallet2 w(cryptonote::MAINNET);
+ w.generate(wallet_file.string(), password);
+ address = w.get_address_as_str();
+ w.store_to(wallet_file.string() + ".keys", password);
+ ASSERT_TRUE(exists(wallet_file));
+ ASSERT_TRUE(exists(wallet_file.string() + ".keys"));
+ }
+
+ tools::wallet2 loaded(cryptonote::MAINNET);
+ loaded.load(wallet_file.string(), password);
+ EXPECT_EQ(loaded.get_address_as_str(), address);
+}
+
+TEST(wallet_storage, change_password_cache_filename_ending_in_keys)
+{
+ const path dir = temp_directory_path() / unique_path("wallet-store-cache-keys-%%%%-%%%%");
+ create_directories(dir);
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ const path wallet_file = dir / "wallet.keys";
+ const path keys_file = dir / "wallet.keys.keys";
+ const epee::wipeable_string password("test");
+ const epee::wipeable_string new_password("new test");
+ std::string address;
+ {
+ tools::wallet2 w(cryptonote::MAINNET);
+ w.generate(keys_file.string(), password);
+ address = w.get_address_as_str();
+ ASSERT_EQ(w.path(), wallet_file.string());
+ w.change_password(w.path(), password, new_password);
+ ASSERT_TRUE(exists(wallet_file));
+ ASSERT_TRUE(exists(keys_file));
+ }
+
+ tools::wallet2 loaded(cryptonote::MAINNET);
+ loaded.load(keys_file.string(), new_password);
+ EXPECT_EQ(loaded.get_address_as_str(), address);
+}
+
+TEST(wallet_storage, store_to_rejects_keys_overlapping_cache)
+{
+ const path fixture = unit_test::data_dir / "wallet_00fd416a";
+ const epee::wipeable_string password("beepbeep");
+ const path dir = temp_directory_path() / unique_path("wallet-store-overlap-%%%%-%%%%");
+ create_directories(dir);
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ for (int i = 0; i != 3; ++i)
+ {
+ SCOPED_TRACE(i);
+ const path target = dir / ("wallet-" + std::to_string(i));
+ const std::string source = target.string() + ".keys";
+ const std::string source_keys = source + ".keys";
+ const path store_path = i == 1 ? dir / "missing" / ".." / target.filename() : target;
+ if (i != 2)
+ tools::copy_file(fixture.string(), source);
+ tools::copy_file(fixture.string() + ".keys", source_keys);
+
+ std::string cache_before, keys_before;
+ if (i != 2)
+ {
+ ASSERT_TRUE(load_file_to_string(source, cache_before));
+ }
+ ASSERT_TRUE(load_file_to_string(source_keys, keys_before));
+ {
+ tools::wallet2 w;
+ w.load(source_keys, password);
+ if (i == 2)
+ {
+ ASSERT_FALSE(exists(source));
+ }
+ EXPECT_THROW(w.store_to(store_path.string(), password), tools::error::wallet_internal_error);
+ }
+ std::string cache_after, keys_after;
+ if (i != 2)
+ {
+ ASSERT_TRUE(load_file_to_string(source, cache_after));
+ EXPECT_EQ(cache_before, cache_after);
+ }
+ else
+ {
+ EXPECT_FALSE(exists(source));
+ }
+ ASSERT_TRUE(exists(source_keys));
+ ASSERT_TRUE(load_file_to_string(source_keys, keys_after));
+ EXPECT_EQ(keys_before, keys_after);
+ EXPECT_FALSE(exists(target));
+ tools::wallet2 loaded;
+ loaded.load(source_keys, password);
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, loaded.get_address_as_str());
+ if (i == 2)
+ {
+ loaded.store_to("", password);
+ EXPECT_TRUE(exists(source));
+ }
+ }
+}
+
+#ifndef WIN32
+TEST(wallet_storage, store_to_rejects_cache_symlink_target)
+{
+ const path fixture = unit_test::data_dir / "wallet_00fd416a";
+ const epee::wipeable_string password("beepbeep");
+ const path dir = temp_directory_path() / unique_path("wallet-store-alias-%%%%-%%%%");
+ create_directories(dir);
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ const path cache = dir / "realwallet";
+ const path link = dir / "link";
+ const path keys = path(link.string() + ".keys");
+ tools::copy_file(fixture.string(), cache.string());
+ tools::copy_file(fixture.string() + ".keys", keys.string());
+ create_symlink(cache, link);
+
+ std::string cache_before, keys_before;
+ ASSERT_TRUE(load_file_to_string(cache.string(), cache_before));
+ ASSERT_TRUE(load_file_to_string(keys.string(), keys_before));
+ {
+ tools::wallet2 w;
+ w.load(link.string(), password);
+ EXPECT_THROW(w.store_to(cache.string(), password), tools::error::wallet_internal_error);
+ }
+ std::string cache_after, keys_after;
+ ASSERT_TRUE(load_file_to_string(cache.string(), cache_after));
+ ASSERT_TRUE(load_file_to_string(keys.string(), keys_after));
+ EXPECT_EQ(cache_before, cache_after);
+ EXPECT_EQ(keys_before, keys_after);
+ EXPECT_TRUE(is_symlink(link));
+ EXPECT_FALSE(exists(cache.string() + ".keys"));
+ tools::wallet2 loaded;
+ loaded.load(link.string(), password);
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, loaded.get_address_as_str());
+}
+
+TEST(wallet_storage, store_to_accepts_directory_symlink_alias)
+{
+ const path fixture = unit_test::data_dir / "wallet_00fd416a";
+ const epee::wipeable_string password("beepbeep");
+ const path dir = temp_directory_path() / unique_path("wallet-store-dir-alias-%%%%-%%%%");
+ create_directories(dir / "actual");
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ const path source = dir / "actual" / "wallet";
+ const path alias = dir / "alias";
+ tools::copy_file(fixture.string(), source.string());
+ tools::copy_file(fixture.string() + ".keys", source.string() + ".keys");
+ create_directory_symlink(dir / "actual", alias);
+ {
+ tools::wallet2 w;
+ w.load(source.string(), password);
+ w.store_to((alias / "wallet").string(), password);
+ }
+ tools::wallet2 loaded;
+ loaded.load((alias / "wallet").string(), password);
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, loaded.get_address_as_str());
+}
+#endif
+
+TEST(wallet_storage, store_to_rejects_ambiguous_keys_cache_alias)
+{
+ const path fixture = unit_test::data_dir / "wallet_00fd416a";
+ const epee::wipeable_string password("beepbeep");
+ const path dir = temp_directory_path() / unique_path("wallet-store-keys-alias-%%%%-%%%%");
+ create_directories(dir);
+ epee::scope_guard cleanup{[&] { boost::system::error_code ec; remove_all(dir, ec); }};
+
+ const path cache = dir / "A.keys";
+ const path keys = path(cache.string() + ".keys");
+ const path alias = dir / "." / "A.keys";
+ tools::copy_file(fixture.string(), cache.string());
+ tools::copy_file(fixture.string() + ".keys", keys.string());
+ std::string cache_before, keys_before;
+ ASSERT_TRUE(load_file_to_string(cache.string(), cache_before));
+ ASSERT_TRUE(load_file_to_string(keys.string(), keys_before));
+ {
+ tools::wallet2 w;
+ w.load(keys.string(), password);
+ EXPECT_THROW(w.store_to(alias.string(), password), tools::error::wallet_internal_error);
+ }
+ std::string cache_after, keys_after;
+ ASSERT_TRUE(load_file_to_string(cache.string(), cache_after));
+ ASSERT_TRUE(load_file_to_string(keys.string(), keys_after));
+ EXPECT_EQ(cache_before, cache_after);
+ EXPECT_EQ(keys_before, keys_after);
+ tools::wallet2 loaded;
+ loaded.load(keys.string(), password);
+ EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, loaded.get_address_as_str());
+}
+
TEST(wallet_storage, store_to_mem2file)
{
for (const auto nettype : {cryptonote::MAINNET, cryptonote::TESTNET, cryptonote::STAGENET})Why this scored 67/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.