lock cs_main for State/Misbehaving

ProcessMessage calls State(...) and Misbehaving(...) without holding the
required lock; add LOCK(cs_main) blocks.

zcash: cherry picked from commit efb54ba065e41e0d36383bcabfcc01bbca7b2340
zcash: https://github.com/bitcoin/bitcoin/pull/7942
This commit is contained in:
Kaz Wesley 2016-04-25 13:13:52 -07:00 committed by Larry Ruane
parent 4798e94186
commit 111eedf7bf
1 changed files with 19 additions and 1 deletions

View File

@ -5607,6 +5607,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
if (pfrom->nVersion != 0)
{
pfrom->PushMessage("reject", strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 1);
return false;
}
@ -5679,7 +5680,10 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
// Potentially mark this peer as a preferred download peer.
UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
{
LOCK(cs_main);
UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
}
// Change version
pfrom->PushMessage("verack");
@ -5743,6 +5747,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
else if (pfrom->nVersion == 0)
{
// Must have a version message before anything else
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 1);
return false;
}
@ -5790,6 +5795,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
return true;
if (vAddr.size() > 1000)
{
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("message addr size() = %u", vAddr.size());
}
@ -5849,6 +5855,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
vRecv >> vInv;
if (vInv.size() > MAX_INV_SZ)
{
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("message inv size() = %u", vInv.size());
}
@ -5915,6 +5922,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
vRecv >> vInv;
if (vInv.size() > MAX_INV_SZ)
{
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("message getdata size() = %u", vInv.size());
}
@ -6156,6 +6164,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
// Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
unsigned int nCount = ReadCompactSize(vRecv);
if (nCount > MAX_HEADERS_RESULTS) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("headers message size = %u", nCount);
}
@ -6416,6 +6425,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
// This isn't a Misbehaving(100) (immediate ban) because the
// peer might be an older or different implementation with
// a different signature key, etc.
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 10);
}
}
@ -6427,6 +6437,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
strCommand == "filteradd"))
{
if (pfrom->nVersion >= NO_BLOOM_VERSION) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
return false;
} else if (GetBoolArg("-enforcenodebloom", DEFAULT_ENFORCENODEBLOOM)) {
@ -6442,8 +6453,11 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
vRecv >> filter;
if (!filter.IsWithinSizeConstraints())
{
// There is no excuse for sending a too-large filter
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
}
else
{
LOCK(pfrom->cs_filter);
@ -6464,13 +6478,17 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
// and thus, the maximum size any matched object can have) in a filteradd message
if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
{
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
} else {
LOCK(pfrom->cs_filter);
if (pfrom->pfilter)
pfrom->pfilter->insert(vData);
else
{
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
}
}
}