aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/scriptpubkeyman.h
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2023-11-02 13:26:57 +0100
committerVasil Dimov <vd@FreeBSD.org>2024-01-18 18:12:59 +0100
commit32a9f13cb805ecf6aebb5cf4e5d92b8a47c4548b (patch)
tree4ab4908ee7d63448a69c929c3cdb7a513ea538f6 /src/wallet/scriptpubkeyman.h
parent03c5b0064d4f766bc8dc6508773c7579e9ad39bc (diff)
downloadbitcoin-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/scriptpubkeyman.h')
-rw-r--r--src/wallet/scriptpubkeyman.h4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h
index 449a75eb6b..b63ba5bda0 100644
--- a/src/wallet/scriptpubkeyman.h
+++ b/src/wallet/scriptpubkeyman.h
@@ -22,6 +22,7 @@
#include <boost/signals2/signal.hpp>
+#include <functional>
#include <optional>
#include <unordered_map>
@@ -46,7 +47,8 @@ public:
virtual void UnsetBlankWalletFlag(WalletBatch&) = 0;
virtual bool CanSupportFeature(enum WalletFeature) const = 0;
virtual void SetMinVersion(enum WalletFeature, WalletBatch* = nullptr) = 0;
- virtual const CKeyingMaterial& GetEncryptionKey() const = 0;
+ //! Pass the encryption key to cb().
+ virtual bool WithEncryptionKey(std::function<bool (const CKeyingMaterial&)> cb) const = 0;
virtual bool HasEncryptionKeys() const = 0;
virtual bool IsLocked() const = 0;
};