wallet2: rewrite keys file in a safer manner

This commit is contained in:
Nathan Dorfman 2018-10-25 18:03:18 -06:00
parent 4cbb476cd1
commit e86af52e2f

View file

@ -3140,13 +3140,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]); crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.iv, &cipher[0]);
keys_file_data.account_data = cipher; keys_file_data.account_data = cipher;
unlock_keys_file(); std::string tmp_file_name = keys_file_name + ".new";
std::string buf; std::string buf;
r = ::serialization::dump_binary(keys_file_data, 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 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 " << keys_file_name); 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(); 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; return true;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------