Zero out wallet master key upon lock

When an encrypted wallet is locked (for instance via the
RPC `walletlock`), the docs indicate that the key is
removed from memory. However, the vector (with a secure
allocator) is merely cleared. This allows the key to persist
indefinitely in memory. Instead, manually fill the bytes with
zeroes before clearing.
This commit is contained in:
John Moffett committed 2023-02-10 20:21:23 -05:00
1 parent b92d609fb2
commit 3a11adc700
1 file changed
+4
+4
View File
@@ -26,6 +26,7 @@
#include <script/descriptor.h> #include <script/descriptor.h>
#include <script/script.h> #include <script/script.h>
#include <script/signingprovider.h> #include <script/signingprovider.h>
#include <support/cleanse.h>
#include <txmempool.h> #include <txmempool.h>
#include <util/bip32.h> #include <util/bip32.h>
#include <util/check.h> #include <util/check.h>
@@ -3407,8 +3408,11 @@ bool CWallet::Lock()
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
if (!vMasterKey.empty()) {
memory_cleanse(vMasterKey.data(), vMasterKey.size() * sizeof(decltype(vMasterKey)::value_type));
vMasterKey.clear(); vMasterKey.clear();
} }
}
NotifyStatusChanged(this); NotifyStatusChanged(this);
return true; return true;