diff options
author | Riccardo Spagni <ric@spagni.net> | 2018-11-06 15:09:45 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2018-11-06 15:09:45 +0200 |
commit | b789f7e10d34c1f38e07e3d42738aa26a87e4513 (patch) | |
tree | cf7bb70cf56d95c5929e418c24602a17843e6780 /src/wallet | |
parent | Merge pull request #4728 (diff) | |
parent | wallet2: rewrite keys file in a safer manner (diff) | |
download | monero-b789f7e10d34c1f38e07e3d42738aa26a87e4513.tar.xz |
Merge pull request #4729
e86af52e wallet2: rewrite keys file in a safer manner (Nathan Dorfman)
Diffstat (limited to 'src/wallet')
-rw-r--r-- | src/wallet/wallet2.cpp | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a90a93321..a5faee71b 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3151,13 +3151,22 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.iv, &cipher[0]); keys_file_data.account_data = cipher; - unlock_keys_file(); + std::string tmp_file_name = keys_file_name + ".new"; std::string buf; r = ::serialization::dump_binary(keys_file_data, buf); - r = r && epee::file_io_utils::save_string_to_file(keys_file_name, buf); //and never touch wallet_keys_file again, only read - CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << keys_file_name); + r = r && epee::file_io_utils::save_string_to_file(tmp_file_name, buf); + CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << tmp_file_name); + + unlock_keys_file(); + std::error_code e = tools::replace_file(tmp_file_name, keys_file_name); lock_keys_file(); + if (e) { + boost::filesystem::remove(tmp_file_name); + LOG_ERROR("failed to update wallet keys file " << keys_file_name); + return false; + } + return true; } //---------------------------------------------------------------------------------------------------- |