wallet2: handle .keys saves and overlapping paths
What changed, and why it matters
This commit fixes a bug in how the Monero wallet saves itself to disk. Previously, the wallet could accidentally overwrite or corrupt its own files when the user chose a path that overlapped with existing wallet files, such as saving to a file whose name ends in '.keys' or using symbolic links. The patch makes the wallet detect these overlapping paths and refuse unsafe saves, while still allowing normal saves. It also adds many automated tests to confirm the behavior.
Treat as a security-hardening fix and include in release notes. Users and integrators running wallet RPC or CLI should upgrade so that wallet save operations cannot corrupt or overwrite keys/cache files through overlapping paths or symlinks. Review any custom tooling that calls store_to() with unusual paths.
Security signals we found
File overwrite / corruption prevention in wallet save path handling
Symlink and hardlink alias detection via filesystem equivalent() checks
Rejection of overlapping source/destination wallet files
New unit tests explicitly exercise malicious/edge path scenarios
Evidence from the diff
The change is in wallet2::store_to(), which writes a wallet’s cache and keys files. The old code compared only the canonical path of the current cache file to the destination path to decide if the save was ‘in place.’ This missed cases where the destination path overlapped with the keys file, where the cache filename itself ended in ‘.keys’, or where symlinks made two paths point to the same inode. The new code computes the intended keys/cache/mms filenames early, checks whether both the cache and keys files would stay the same entry (using filename, parent-path canonicalization, and boost::filesystem::equivalent), and rejects any save where an old file would overlap a new file. It also updates internal filenames before writing and adds unit tests covering keys-named files, current-keys saves, cache filenames ending in ‘.keys’, overlapping paths, symlink aliases, and ambiguous aliases.
Changed components
src/wallet/wallet2.cpptests/unit_tests/wallet_storage.cppInspect 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>
@@ -7199,13 +7200,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,
@@ -7246,15 +7280,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 65/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.