Skip to content

Commit 1a655e8

Browse files
author
MarcoFalke
committed
Merge #19514: [net/net processing] check banman pointer before dereferencing
ca3585a [net/net processing] check banman pointer before dereferencing (John Newbery) Pull request description: Although we currently don't do this, it should be possible to create a CConnman or PeerLogicValidation without a Banman instance. Therefore always check that banman exists before dereferencing the pointer. Also add comments to the m_banman members of CConnman and PeerLogicValidation to document that these may be nullptr. ACKs for top commit: jonatack: ACK ca3585a theStack: ACK bitcoin/bitcoin@ca3585a Tree-SHA512: 726401c8921b9a502029ead34ae797473a1bc359d6e4e58dcbe3e25b70dde40bb100723be467fd3e2bf418892c493911998226de19c9d529d72034e3be26be48
2 parents 07c83ce + ca3585a commit 1a655e8

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10131013
SetSocketNoDelay(hSocket);
10141014

10151015
// Don't accept connections from banned peers.
1016-
bool banned = m_banman->IsBanned(addr);
1016+
bool banned = m_banman && m_banman->IsBanned(addr);
10171017
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::PF_NOBAN) && banned)
10181018
{
10191019
LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString());
@@ -1022,7 +1022,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10221022
}
10231023

10241024
// Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
1025-
bool discouraged = m_banman->IsDiscouraged(addr);
1025+
bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
10261026
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::PF_NOBAN) && nInbound + 1 >= nMaxInbound && discouraged)
10271027
{
10281028
LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToString());

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ class CConnman
447447
std::atomic<int> nBestHeight;
448448
CClientUIInterface* clientInterface;
449449
NetEventsInterface* m_msgproc;
450+
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
450451
BanMan* m_banman;
451452

452453
/** SipHasher seeds for deterministic randomness */

src/net_processing.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,8 +2491,10 @@ void ProcessMessage(
24912491
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
24922492
addr.nTime = nNow - 5 * 24 * 60 * 60;
24932493
pfrom.AddAddressKnown(addr);
2494-
if (banman->IsDiscouraged(addr)) continue; // Do not process banned/discouraged addresses beyond remembering we received them
2495-
if (banman->IsBanned(addr)) continue;
2494+
if (banman && (banman->IsDiscouraged(addr) || banman->IsBanned(addr))) {
2495+
// Do not process banned/discouraged addresses beyond remembering we received them
2496+
continue;
2497+
}
24962498
bool fReachable = IsReachable(addr);
24972499
if (addr.nTime > nSince && !pfrom.fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
24982500
{
@@ -3346,7 +3348,8 @@ void ProcessMessage(
33463348
std::vector<CAddress> vAddr = connman->GetAddresses();
33473349
FastRandomContext insecure_rand;
33483350
for (const CAddress &addr : vAddr) {
3349-
if (!banman->IsDiscouraged(addr) && !banman->IsBanned(addr)) {
3351+
bool banned_or_discouraged = banman && (banman->IsDiscouraged(addr) || banman->IsBanned(addr));
3352+
if (!banned_or_discouraged) {
33503353
pfrom.PushAddress(addr, insecure_rand);
33513354
}
33523355
}

src/net_processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static const int DISCOURAGEMENT_THRESHOLD{100};
2929
class PeerLogicValidation final : public CValidationInterface, public NetEventsInterface {
3030
private:
3131
CConnman* const connman;
32+
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
3233
BanMan* const m_banman;
3334
ChainstateManager& m_chainman;
3435
CTxMemPool& m_mempool;

0 commit comments

Comments
 (0)