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.

Github-Pull: #27080
Rebased-From: 3a11adc700
This commit is contained in:
John Moffett 2023-02-10 16:13:40 -05:00 committed by fanquake
parent b7e242ecb3
commit 64e7db6f4f
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1

View file

@ -25,6 +25,7 @@
#include <script/descriptor.h>
#include <script/script.h>
#include <script/signingprovider.h>
#include <support/cleanse.h>
#include <txmempool.h>
#include <util/bip32.h>
#include <util/check.h>
@ -3293,7 +3294,10 @@ bool CWallet::Lock()
{
LOCK(cs_wallet);
vMasterKey.clear();
if (!vMasterKey.empty()) {
memory_cleanse(vMasterKey.data(), vMasterKey.size() * sizeof(decltype(vMasterKey)::value_type));
vMasterKey.clear();
}
}
NotifyStatusChanged(this);