eliminate races: hold cs_KeyStore lock while reading fUseCrypto

This commit is contained in:
Larry Ruane 2019-04-08 11:02:08 -06:00
parent 5a5094bbb5
commit d9fcc2b558
2 changed files with 132 additions and 174 deletions

View File

@ -185,24 +185,22 @@ static bool DecryptSaplingSpendingKey(const CKeyingMaterial& vMasterKey,
return sk.expsk.full_viewing_key() == extfvk.fvk; return sk.expsk.full_viewing_key() == extfvk.fvk;
} }
// cs_KeyStore lock must be held by caller
bool CCryptoKeyStore::SetCrypted() bool CCryptoKeyStore::SetCrypted()
{ {
LOCK(cs_KeyStore);
if (fUseCrypto) if (fUseCrypto)
return true; return true;
if (!(mapKeys.empty() && mapSproutSpendingKeys.empty() && mapSaplingSpendingKeys.empty())) if (!(mapKeys.empty() && mapSproutSpendingKeys.empty() && mapSaplingSpendingKeys.empty()))
return false; return false;
fUseCrypto = true; return fUseCrypto = true;
return true;
} }
bool CCryptoKeyStore::Lock() bool CCryptoKeyStore::Lock()
{ {
if (!SetCrypted())
return false;
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!SetCrypted())
return false;
vMasterKey.clear(); vMasterKey.clear();
} }
@ -291,7 +289,7 @@ bool CCryptoKeyStore::SetHDSeed(const HDSeed& seed)
{ {
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!IsCrypted()) { if (!fUseCrypto) {
return CBasicKeyStore::SetHDSeed(seed); return CBasicKeyStore::SetHDSeed(seed);
} }
@ -316,27 +314,25 @@ bool CCryptoKeyStore::SetCryptedHDSeed(
const uint256& seedFp, const uint256& seedFp,
const std::vector<unsigned char>& vchCryptedSecret) const std::vector<unsigned char>& vchCryptedSecret)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto) {
if (!IsCrypted()) { return false;
return false;
}
if (!cryptedHDSeed.first.IsNull()) {
// Don't allow an existing seed to be changed. We can maybe relax this
// restriction later once we have worked out the UX implications.
return false;
}
cryptedHDSeed = std::make_pair(seedFp, vchCryptedSecret);
} }
if (!cryptedHDSeed.first.IsNull()) {
// Don't allow an existing seed to be changed. We can maybe relax this
// restriction later once we have worked out the UX implications.
return false;
}
cryptedHDSeed = std::make_pair(seedFp, vchCryptedSecret);
return true; return true;
} }
bool CCryptoKeyStore::HaveHDSeed() const bool CCryptoKeyStore::HaveHDSeed() const
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!IsCrypted()) if (!fUseCrypto)
return CBasicKeyStore::HaveHDSeed(); return CBasicKeyStore::HaveHDSeed();
return !cryptedHDSeed.second.empty(); return !cryptedHDSeed.second.empty();
@ -345,7 +341,7 @@ bool CCryptoKeyStore::HaveHDSeed() const
bool CCryptoKeyStore::GetHDSeed(HDSeed& seedOut) const bool CCryptoKeyStore::GetHDSeed(HDSeed& seedOut) const
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!IsCrypted()) if (!fUseCrypto)
return CBasicKeyStore::GetHDSeed(seedOut); return CBasicKeyStore::GetHDSeed(seedOut);
if (cryptedHDSeed.second.empty()) if (cryptedHDSeed.second.empty())
@ -356,125 +352,106 @@ bool CCryptoKeyStore::GetHDSeed(HDSeed& seedOut) const
bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::AddKeyPubKey(key, pubkey);
return CBasicKeyStore::AddKeyPubKey(key, pubkey);
if (IsLocked()) if (IsLocked())
return false; return false;
std::vector<unsigned char> vchCryptedSecret; std::vector<unsigned char> vchCryptedSecret;
CKeyingMaterial vchSecret(key.begin(), key.end()); CKeyingMaterial vchSecret(key.begin(), key.end());
if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret)) if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret))
return false; return false;
if (!AddCryptedKey(pubkey, vchCryptedSecret)) return AddCryptedKey(pubkey, vchCryptedSecret);
return false;
}
return true;
} }
bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!SetCrypted())
if (!SetCrypted()) return false;
return false;
mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret); mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
}
return true; return true;
} }
bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::GetKey(address, keyOut);
return CBasicKeyStore::GetKey(address, keyOut);
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end()) if (mi != mapCryptedKeys.end())
{ {
const CPubKey &vchPubKey = (*mi).second.first; const CPubKey &vchPubKey = (*mi).second.first;
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut); return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
}
} }
return false; return false;
} }
bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CKeyStore::GetPubKey(address, vchPubKeyOut);
return CKeyStore::GetPubKey(address, vchPubKeyOut);
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end()) if (mi != mapCryptedKeys.end())
{ {
vchPubKeyOut = (*mi).second.first; vchPubKeyOut = (*mi).second.first;
return true; return true;
}
} }
return false; return false;
} }
bool CCryptoKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk) bool CCryptoKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::AddSproutSpendingKey(sk);
return CBasicKeyStore::AddSproutSpendingKey(sk);
if (IsLocked()) if (IsLocked())
return false; return false;
std::vector<unsigned char> vchCryptedSecret; std::vector<unsigned char> vchCryptedSecret;
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION); CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << sk; ss << sk;
CKeyingMaterial vchSecret(ss.begin(), ss.end()); CKeyingMaterial vchSecret(ss.begin(), ss.end());
auto address = sk.address(); auto address = sk.address();
if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret)) if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret))
return false; return false;
if (!AddCryptedSproutSpendingKey(address, sk.receiving_key(), vchCryptedSecret)) return AddCryptedSproutSpendingKey(address, sk.receiving_key(), vchCryptedSecret);
return false;
}
return true;
} }
bool CCryptoKeyStore::AddSaplingSpendingKey( bool CCryptoKeyStore::AddSaplingSpendingKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const libzcash::SaplingPaymentAddress &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto) {
if (!IsCrypted()) { return CBasicKeyStore::AddSaplingSpendingKey(sk, defaultAddr);
return CBasicKeyStore::AddSaplingSpendingKey(sk, defaultAddr);
}
if (IsLocked()) {
return false;
}
std::vector<unsigned char> vchCryptedSecret;
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << sk;
CKeyingMaterial vchSecret(ss.begin(), ss.end());
auto extfvk = sk.ToXFVK();
if (!EncryptSecret(vMasterKey, vchSecret, extfvk.fvk.GetFingerprint(), vchCryptedSecret)) {
return false;
}
if (!AddCryptedSaplingSpendingKey(extfvk, vchCryptedSecret, defaultAddr)) {
return false;
}
} }
return true;
if (IsLocked()) {
return false;
}
std::vector<unsigned char> vchCryptedSecret;
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << sk;
CKeyingMaterial vchSecret(ss.begin(), ss.end());
auto extfvk = sk.ToXFVK();
if (!EncryptSecret(vMasterKey, vchSecret, extfvk.fvk.GetFingerprint(), vchCryptedSecret)) {
return false;
}
return AddCryptedSaplingSpendingKey(extfvk, vchCryptedSecret, defaultAddr);
} }
bool CCryptoKeyStore::AddCryptedSproutSpendingKey( bool CCryptoKeyStore::AddCryptedSproutSpendingKey(
@ -482,14 +459,12 @@ bool CCryptoKeyStore::AddCryptedSproutSpendingKey(
const libzcash::ReceivingKey &rk, const libzcash::ReceivingKey &rk,
const std::vector<unsigned char> &vchCryptedSecret) const std::vector<unsigned char> &vchCryptedSecret)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!SetCrypted())
if (!SetCrypted()) return false;
return false;
mapCryptedSproutSpendingKeys[address] = vchCryptedSecret; mapCryptedSproutSpendingKeys[address] = vchCryptedSecret;
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(rk))); mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(rk)));
}
return true; return true;
} }
@ -498,51 +473,45 @@ bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(
const std::vector<unsigned char> &vchCryptedSecret, const std::vector<unsigned char> &vchCryptedSecret,
const libzcash::SaplingPaymentAddress &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!SetCrypted()) {
if (!SetCrypted()) { return false;
return false;
}
// if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
if (!AddSaplingFullViewingKey(extfvk.fvk, defaultAddr)) {
return false;
}
mapCryptedSaplingSpendingKeys[extfvk] = vchCryptedSecret;
} }
// if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
if (!AddSaplingFullViewingKey(extfvk.fvk, defaultAddr)) {
return false;
}
mapCryptedSaplingSpendingKeys[extfvk] = vchCryptedSecret;
return true; return true;
} }
bool CCryptoKeyStore::GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const bool CCryptoKeyStore::GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::GetSproutSpendingKey(address, skOut);
return CBasicKeyStore::GetSproutSpendingKey(address, skOut);
CryptedSproutSpendingKeyMap::const_iterator mi = mapCryptedSproutSpendingKeys.find(address); CryptedSproutSpendingKeyMap::const_iterator mi = mapCryptedSproutSpendingKeys.find(address);
if (mi != mapCryptedSproutSpendingKeys.end()) if (mi != mapCryptedSproutSpendingKeys.end())
{ {
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
return DecryptSproutSpendingKey(vMasterKey, vchCryptedSecret, address, skOut); return DecryptSproutSpendingKey(vMasterKey, vchCryptedSecret, address, skOut);
}
} }
return false; return false;
} }
bool CCryptoKeyStore::GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingExtendedSpendingKey &skOut) const bool CCryptoKeyStore::GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingExtendedSpendingKey &skOut) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::GetSaplingSpendingKey(fvk, skOut);
return CBasicKeyStore::GetSaplingSpendingKey(fvk, skOut);
for (auto entry : mapCryptedSaplingSpendingKeys) { for (auto entry : mapCryptedSaplingSpendingKeys) {
if (entry.first.fvk == fvk) { if (entry.first.fvk == fvk) {
const std::vector<unsigned char> &vchCryptedSecret = entry.second; const std::vector<unsigned char> &vchCryptedSecret = entry.second;
return DecryptSaplingSpendingKey(vMasterKey, vchCryptedSecret, entry.first, skOut); return DecryptSaplingSpendingKey(vMasterKey, vchCryptedSecret, entry.first, skOut);
}
} }
} }
return false; return false;
@ -552,7 +521,7 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
{ {
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!mapCryptedKeys.empty() || IsCrypted()) if (!mapCryptedKeys.empty() || fUseCrypto)
return false; return false;
fUseCrypto = true; fUseCrypto = true;

View File

@ -157,19 +157,14 @@ public:
bool IsCrypted() const bool IsCrypted() const
{ {
LOCK(cs_KeyStore);
return fUseCrypto; return fUseCrypto;
} }
bool IsLocked() const bool IsLocked() const
{ {
if (!IsCrypted()) LOCK(cs_KeyStore);
return false; return fUseCrypto && vMasterKey.empty();
bool result;
{
LOCK(cs_KeyStore);
result = vMasterKey.empty();
}
return result;
} }
bool Lock(); bool Lock();
@ -183,19 +178,17 @@ public:
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey); bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
bool HaveKey(const CKeyID &address) const bool HaveKey(const CKeyID &address) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::HaveKey(address);
return CBasicKeyStore::HaveKey(address); return mapCryptedKeys.count(address) > 0;
return mapCryptedKeys.count(address) > 0;
}
return false;
} }
bool GetKey(const CKeyID &address, CKey& keyOut) const; bool GetKey(const CKeyID &address, CKey& keyOut) const;
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const; bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
void GetKeys(std::set<CKeyID> &setAddress) const void GetKeys(std::set<CKeyID> &setAddress) const
{ {
if (!IsCrypted()) LOCK(cs_KeyStore);
if (!fUseCrypto)
{ {
CBasicKeyStore::GetKeys(setAddress); CBasicKeyStore::GetKeys(setAddress);
return; return;
@ -215,18 +208,16 @@ public:
bool AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk); bool AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk);
bool HaveSproutSpendingKey(const libzcash::SproutPaymentAddress &address) const bool HaveSproutSpendingKey(const libzcash::SproutPaymentAddress &address) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::HaveSproutSpendingKey(address);
return CBasicKeyStore::HaveSproutSpendingKey(address); return mapCryptedSproutSpendingKeys.count(address) > 0;
return mapCryptedSproutSpendingKeys.count(address) > 0;
}
return false;
} }
bool GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const; bool GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const;
void GetSproutPaymentAddresses(std::set<libzcash::SproutPaymentAddress> &setAddress) const void GetSproutPaymentAddresses(std::set<libzcash::SproutPaymentAddress> &setAddress) const
{ {
if (!IsCrypted()) LOCK(cs_KeyStore);
if (!fUseCrypto)
{ {
CBasicKeyStore::GetSproutPaymentAddresses(setAddress); CBasicKeyStore::GetSproutPaymentAddresses(setAddress);
return; return;
@ -249,14 +240,12 @@ public:
const libzcash::SaplingPaymentAddress &defaultAddr); const libzcash::SaplingPaymentAddress &defaultAddr);
bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const
{ {
{ LOCK(cs_KeyStore);
LOCK(cs_KeyStore); if (!fUseCrypto)
if (!IsCrypted()) return CBasicKeyStore::HaveSaplingSpendingKey(fvk);
return CBasicKeyStore::HaveSaplingSpendingKey(fvk); for (auto entry : mapCryptedSaplingSpendingKeys) {
for (auto entry : mapCryptedSaplingSpendingKeys) { if (entry.first.fvk == fvk) {
if (entry.first.fvk == fvk) { return true;
return true;
}
} }
} }
return false; return false;