diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2023-11-02 13:26:57 +0100 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2024-01-18 18:12:59 +0100 |
commit | 32a9f13cb805ecf6aebb5cf4e5d92b8a47c4548b (patch) | |
tree | 4ab4908ee7d63448a69c929c3cdb7a513ea538f6 /src/wallet/wallet.cpp | |
parent | 03c5b0064d4f766bc8dc6508773c7579e9ad39bc (diff) | |
download | bitcoin-32a9f13cb805ecf6aebb5cf4e5d92b8a47c4548b.tar.xz |
wallet: avoid returning a reference to vMasterKey after releasing the mutex that guards it
`CWallet::GetEncryptionKey()` would return a reference to the internal
`CWallet::vMasterKey`, guarded by `CWallet::cs_wallet`, which is unsafe.
Returning a copy would be a shorter solution, but could have security
implications of the master key remaining somewhere in the memory even
after `CWallet::Lock()` (the current calls to
`CWallet::GetEncryptionKey()` are safe, but that is not future proof).
So, instead of `EncryptSecret(m_storage.GetEncryptionKey(), ...)`
change the `GetEncryptionKey()` method to provide the encryption
key to a given callback:
`m_storage.WithEncryptionKey([](const CKeyingMaterial& k) { EncryptSecret(k, ...); })`
This silences the following (clang 18):
```
wallet/wallet.cpp:3520:12: error: returning variable 'vMasterKey' by reference requires holding mutex 'cs_wallet' [-Werror,-Wthread-safety-reference-return]
3520 | return vMasterKey;
| ^
```
Diffstat (limited to 'src/wallet/wallet.cpp')
-rw-r--r-- | src/wallet/wallet.cpp | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e03f5532fc..e93cd4b4b9 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3513,9 +3513,10 @@ void CWallet::SetupLegacyScriptPubKeyMan() AddScriptPubKeyMan(id, std::move(spk_manager)); } -const CKeyingMaterial& CWallet::GetEncryptionKey() const +bool CWallet::WithEncryptionKey(std::function<bool (const CKeyingMaterial&)> cb) const { - return vMasterKey; + LOCK(cs_wallet); + return cb(vMasterKey); } bool CWallet::HasEncryptionKeys() const |