From d925be57e22213554b0ab2a64d7618611fedf81d Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Fri, 7 Aug 2026 10:55:50 -0300 Subject: wallet: cleanse unlocked secrets before clearing them (#2) --- src/db.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/db.cpp') diff --git a/src/db.cpp b/src/db.cpp index c8642e9..244d5a7 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -1404,6 +1404,13 @@ static bool EncryptPrivateKeyForWallet(const CKeyingMaterial& vchMasterKey, return true; } +static void SecureClearBytes(vector& vch) +{ + if (!vch.empty()) + OPENSSL_cleanse(&vch[0], vch.size()); + vch.clear(); +} + bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& strErrorRet) { strBackupRet.clear(); @@ -1423,6 +1430,7 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st map, CPrivKey> mapPlainKeys; vector vchPlainHDMaster; vector vchPlainHDChainCode; + bool fHadHDSeed = false; unsigned int nHDNextSnapshot = 0; { CRITICAL_BLOCK(cs_mapKeys) @@ -1433,6 +1441,7 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st { vchPlainHDMaster = vchHDMaster; vchPlainHDChainCode = vchHDChainCode; + fHadHDSeed = !vchPlainHDMaster.empty() || !vchPlainHDChainCode.empty(); nHDNextSnapshot = nHDNext; } } @@ -1483,11 +1492,13 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st vector vchCryptedHDMasterNew; vector vchCryptedHDChainCodeNew; - if (!vchPlainHDMaster.empty() || !vchPlainHDChainCode.empty()) + if (fHadHDSeed) { if (vchPlainHDMaster.size() != 32 || vchPlainHDChainCode.size() != 32) { strErrorRet = "wallet has an incomplete HD seed"; + SecureClearBytes(vchPlainHDMaster); + SecureClearBytes(vchPlainHDChainCode); return false; } if (!EncryptSecret(vchMasterKey, vchPlainHDMaster, @@ -1496,8 +1507,12 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st WalletSecretIV("hdchaincode"), vchCryptedHDChainCodeNew)) { strErrorRet = "could not encrypt the HD seed"; + SecureClearBytes(vchPlainHDMaster); + SecureClearBytes(vchPlainHDChainCode); return false; } + SecureClearBytes(vchPlainHDMaster); + SecureClearBytes(vchPlainHDChainCode); } vector > > vNewPool; @@ -1506,7 +1521,7 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st for (int i = 0; i < KEYPOOL_SIZE; i++) { CKey key; - if (!vchPlainHDMaster.empty()) + if (fHadHDSeed) { string strDeriveError; if (!DeriveHDKey(nHDNextNew, key, strDeriveError)) -- cgit v1.2.3