Merge bitcoin/bitcoin#35919: p2p: cap maxconnections at 2,000 to reduce crash risk
What changed, and why it matters
This update limits how many peer connections Bitcoin Core will allow a user to request. Previously, a user could set an extremely high number of connections, which could trigger a crash when the orphan transaction cache (used for unconfirmed transactions) was shared across too many peers. Now, values above 2,000 are automatically reduced to 2,000 with a warning. It is a hardening fix, not a remote exploit.
No urgent action required. Operators who previously set -maxconnections above 2,000 should note the new cap and warning. Review whether the cap adequately protects nodes with very large file-descriptor limits and consider monitoring logs for the new InitWarning.
Security signals we found
Crash risk reduction at very high peer counts
Configuration input validation and clamping
Static assertion linking connection caps to orphanage latency budget
New functional test for cap behavior and warning message
Evidence from the diff
The patch caps the -maxconnections configuration option at MAX_PEER_CONNECTIONS = 2,000. It adds a static_assert ensuring MAX_PEER_CONNECTIONS + MAX_ADDNODE_CONNECTIONS + MAX_PRIVATE_BROADCAST_CONNECTIONS stays below DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE (3,000), because the orphanage divides its global latency budget by the peer count and could abort if the per-peer share became zero. The change updates help text, adds an InitWarning when clamping, and adds functional tests verifying the cap and warning.
Changed components
src/init.cppsrc/net.hsrc/node/txorphanage.htest/functional/feature_init.pyInspect captured patch +29 / −4
### doc/release-notes-35919.md
@@ -0,0 +1,5 @@
+P2P and network changes
+-----------------------
+
+- `-maxconnections` now clamps values above 2,000 with a warning to reduce
+ crash risk at very high peer counts.
### src/init.cpp
@@ -572,10 +572,10 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
argsman.AddArg("-forcednsseed", strprintf("Always query for peer addresses via DNS lookup (default: %u)", DEFAULT_FORCEDNSSEED), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-listen", strprintf("Accept connections from outside (default: %u if no -proxy, -connect or -maxconnections=0)", DEFAULT_LISTEN), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-listenonion", strprintf("Automatically create Tor onion service (default: %d)", DEFAULT_LISTEN_ONION), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
- argsman.AddArg("-maxconnections=<n>", strprintf("Maintain at most <n> automatic connections to peers (default: %u). %u slots of these are reserved for outgoing connections. See -inboundrelaypercent for more information about limits applied to transaction relay inbound peers. "
+ argsman.AddArg("-maxconnections=<n>", strprintf("Maintain at most <n> automatic connections to peers (default: %u, maximum: %u). %u slots of these are reserved for outgoing connections. See -inboundrelaypercent for more information about limits applied to transaction relay inbound peers. "
"This limit does not apply to connections manually added via -addnode or the addnode RPC, which have a separate limit of %u. "
"It does not apply to short-lived private broadcast connections either, which have a separate limit of %u.",
- DEFAULT_MAX_PEER_CONNECTIONS, MAX_OUTBOUND_FULL_RELAY_CONNECTIONS + MAX_BLOCK_RELAY_ONLY_CONNECTIONS + MAX_FEELER_CONNECTIONS, MAX_ADDNODE_CONNECTIONS, MAX_PRIVATE_BROADCAST_CONNECTIONS),
+ DEFAULT_MAX_PEER_CONNECTIONS, MAX_PEER_CONNECTIONS, MAX_OUTBOUND_FULL_RELAY_CONNECTIONS + MAX_BLOCK_RELAY_ONLY_CONNECTIONS + MAX_FEELER_CONNECTIONS, MAX_ADDNODE_CONNECTIONS, MAX_PRIVATE_BROADCAST_CONNECTIONS),
ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-inboundrelaypercent=<n>", strprintf("Permit a maximum percent of inbound connections to relay transactions, to limit memory utilization (0 to 100, default: %u).", DEFAULT_FULL_RELAY_INBOUND_PCT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-maxreceivebuffer=<n>", strprintf("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXRECEIVEBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1060,10 +1060,14 @@ bool AppInitParameterInteraction(const ArgsManager& args)
// Number of bound interfaces (we have at least one)
int num_p2p_bind = std::max(num_user_p2p_bind, size_t(1));
// Maximum number of connections with other nodes, this accounts for all types of outbounds and inbounds except for manual
- int user_p2p_max_connections = args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
+ int user_p2p_max_connections = args.GetArg<int>("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
if (user_p2p_max_connections < 0) {
return InitError(Untranslated("-maxconnections must be greater or equal than zero"));
}
+ if (user_p2p_max_connections > MAX_PEER_CONNECTIONS) {
+ InitWarning(strprintf(_("Reducing -maxconnections from %d to %d."), user_p2p_max_connections, MAX_PEER_CONNECTIONS));
+ user_p2p_max_connections = MAX_PEER_CONNECTIONS;
+ }
// Reserve one listening socket per -ipcbind address plus its accepted
// connection slots. Unlike P2P, IPC has no default listener.
### src/net.h
@@ -80,6 +80,8 @@ inline constexpr size_t MAX_PRIVATE_BROADCAST_CONNECTIONS{64};
inline constexpr bool DEFAULT_LISTEN = true;
/** The maximum number of peer connections to maintain. */
inline constexpr unsigned int DEFAULT_MAX_PEER_CONNECTIONS{200};
+/** Maximum value of -maxconnections */
+inline constexpr int MAX_PEER_CONNECTIONS{2'000};
/** Default percentage of inbound connection slots that tx-relaying peers can use */
inline constexpr int DEFAULT_FULL_RELAY_INBOUND_PCT{50};
/** The default for -maxuploadtarget. 0 = Unlimited */
### src/node/txorphanage.h
@@ -20,7 +20,10 @@ namespace node {
inline constexpr int64_t DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER{404'000};
/** Default value for TxOrphanage::m_max_global_latency_score. Helps limit the maximum latency for operations like
* EraseForBlock and LimitOrphans. */
-inline constexpr unsigned int DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE{3000};
+inline constexpr unsigned int DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE{3'000};
+
+// Keep the connection caps below the default global latency limit to avoid a zero per-peer share.
+static_assert(MAX_PEER_CONNECTIONS + MAX_ADDNODE_CONNECTIONS + MAX_PRIVATE_BROADCAST_CONNECTIONS < DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
/** A class to track orphan transactions (failed on TX_MISSING_INPUTS)
* Since we cannot distinguish orphans from bad transactions with non-existent inputs, we heavily limit the amount of
### test/functional/feature_init.py
@@ -386,10 +386,21 @@ def init_fd_overflow_test(self):
# No HTTP server, no RPC `stop`
sigterm_node(node)
+ # From net.h
+ MAX_PEER_CONNECTIONS = 2000
+
+ self.log.info("Checking -maxconnections is capped before reserving file descriptors")
+ for requested, parsed in [(MAX_PEER_CONNECTIONS + 1, MAX_PEER_CONNECTIONS + 1), (2**64, 2**31 - 1)]:
+ warning = f"Reducing -maxconnections from {parsed} to {MAX_PEER_CONNECTIONS}."
+ self.restart_node(1, extra_args=[f"-maxconnections={requested}"])
+ self.stop_node(1, expected_stderr=re.compile(re.escape(warning)))
+
if self.RLIM_INFINITY is not None:
# Get the platform's file descriptor limit, if possible
import resource
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
+ # Stay within the -maxconnections cap so the descriptor limit is what reduces it
+ soft = min(soft, MAX_PEER_CONNECTIONS)
# Lower the hard limit so RaiseFileDescriptorLimit() has a ceiling.
# The hard limit can not be raised again without root privilges,Why this scored 39/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.