Skip to content

Commit

Permalink
net: Better misbehaving logging
Browse files Browse the repository at this point in the history
Adaptation of btc@d3a185a33b7bc09e4ca998f42f1f9aea8177ef8a

This moves the error messages for misbehavior (when available) into the
line that reports the misbehavior, as well as moves the logging to the
`net` category.

This is a continuation of btc#11583 and avoids serious-looking errors due
to misbehaving peers.

To do this, Misbehaving() gains an optional `message` argument.

E.g. change:

2018-01-18 16:02:27 Misbehaving: x.x.x.x:62174 peer=164603 (80 -> 100) BAN THRESHOLD EXCEEDED
2018-01-18 16:02:27 ERROR: non-continuous headers sequence

to

2018-01-18 16:02:27 Misbehaving: x.x.x.x:62174 peer=164603 (80 -> 100) BAN THRESHOLD EXCEEDED: non-continuous headers sequence
  • Loading branch information
furszy committed Aug 10, 2021
1 parent 3660487 commit c3c04e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
54 changes: 29 additions & 25 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,22 +583,24 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
}

// Requires cs_main.
void Misbehaving(NodeId pnode, int howmuch) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void Misbehaving(NodeId pnode, int howmuch, const std::string& message) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
if (howmuch == 0)
return;

CNodeState* state = State(pnode);
if (state == NULL)
if (state == nullptr)
return;

state->nMisbehavior += howmuch;
int banscore = gArgs.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
std::string message_prefixed = message.empty() ? "" : (": " + message);
if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore) {
LogPrintf("Misbehaving: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", state->name, state->nMisbehavior - howmuch, state->nMisbehavior);
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
state->fShouldBan = true;
} else
LogPrintf("Misbehaving: %s (%d -> %d)\n", state->name, state->nMisbehavior - howmuch, state->nMisbehavior);
} else {
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d)%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
}
}

static void CheckBlockSpam(NodeId nodeId, const uint256& hashBlock)
Expand Down Expand Up @@ -1277,8 +1279,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
return true;
if (vAddr.size() > MAX_ADDR_TO_SEND) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("message addr size() = %u", vAddr.size());
Misbehaving(pfrom->GetId(), 20, strprintf("message addr size() = %u", vAddr.size()));
return false;
}

// Store the new addresses
Expand Down Expand Up @@ -1317,8 +1319,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
vRecv >> vInv;
if (vInv.size() > MAX_INV_SZ) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("peer=%d message inv size() = %u", pfrom->GetId(), vInv.size());
Misbehaving(pfrom->GetId(), 20, strprintf("message inv size() = %u", vInv.size()));
return false;
}

LOCK(cs_main);
Expand All @@ -1333,8 +1335,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR

// Reject deprecated messages
if (inv.type == MSG_TXLOCK_REQUEST || inv.type == MSG_TXLOCK_VOTE) {
Misbehaving(pfrom->GetId(), 20);
return error("message inv deprecated %d", (int)inv.type);
Misbehaving(pfrom->GetId(), 100, strprintf("message inv deprecated %d", (int)inv.type));
return false;
}

pfrom->AddInventoryKnown(inv);
Expand Down Expand Up @@ -1367,8 +1369,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
vRecv >> vInv;
if (vInv.size() > MAX_INV_SZ) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("peer=%d message getdata size() = %u", pfrom->GetId(), vInv.size());
Misbehaving(pfrom->GetId(), 20, strprintf("message getdata size() = %u", vInv.size()));
return false;
}

if (vInv.size() != 1)
Expand Down Expand Up @@ -1483,8 +1485,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR

if (ptx->ContainsZerocoins()) {
// Don't even try to check zerocoins at all.
Misbehaving(pfrom->GetId(), 100);
LogPrint(BCLog::NET, " misbehaving peer, received a zc transaction, peer: %s\n", pfrom->GetAddrName());
Misbehaving(pfrom->GetId(), 100, strprintf("received a zc transaction"));
return false;
}

if (AcceptToMemoryPool(mempool, state, ptx, true, &fMissingInputs, false, ignoreFees)) {
Expand Down Expand Up @@ -1622,8 +1624,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
unsigned int nCount = ReadCompactSize(vRecv);
if (nCount > MAX_HEADERS_RESULTS) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
return error("headers message size = %u", nCount);
Misbehaving(pfrom->GetId(), 20, strprintf("headers message size = %u", nCount));
return false;
}
headers.resize(nCount);
for (unsigned int n = 0; n < nCount; n++) {
Expand All @@ -1641,8 +1643,8 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
for (const CBlockHeader& header : headers) {
CValidationState state;
if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
Misbehaving(pfrom->GetId(), 20);
return error("non-continuous headers sequence");
Misbehaving(pfrom->GetId(), 20, "non-continuous headers sequence");
return false;
}

/*TODO: this has a CBlock cast on it so that it will compile. There should be a solution for this
Expand All @@ -1651,10 +1653,12 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
if (!AcceptBlockHeader((CBlock)header, state, &pindexLast)) {
int nDoS;
if (state.IsInvalid(nDoS)) {
if (nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS);
std::string strError = "invalid header received " + header.GetHash().ToString();
return error(strError.c_str());
if (nDoS > 0) {
Misbehaving(pfrom->GetId(), nDoS, "invalid header received");
} else {
LogPrint(BCLog::NET, "peer=%d: invalid header received\n", pfrom->GetId());
}
return false;
}
}
}
Expand Down Expand Up @@ -1818,9 +1822,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
(strCommand == NetMsgType::FILTERLOAD ||
strCommand == NetMsgType::FILTERADD ||
strCommand == NetMsgType::FILTERCLEAR)) {
LogPrintf("bloom message=%s\n", strCommand);
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
Misbehaving(pfrom->GetId(), 100, "banning, filter received.");
return false;
}

else if (strCommand == NetMsgType::FILTERLOAD) {
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ struct CNodeStateStats {
/** Get statistics from node state */
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
/** Increase a node's misbehavior score. */
void Misbehaving(NodeId nodeid, int howmuch) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main);

#endif // BITCOIN_NET_PROCESSING_H

0 comments on commit c3c04e4

Please sign in to comment.