Skip to content

Commit 9898400

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#14532: Never bind INADDR_ANY by default, and warn when doing so explicitly
27c44ef rpcbind: Warn about exposing RPC to untrusted networks (Luke Dashjr) d6a1287 CNetAddr: Add IsBindAny method to check for INADDR_ANY (Luke Dashjr) 3615003 net: Always default rpcbind to localhost, never "all interfaces" (Luke Dashjr) Pull request description: A disturbingly large number of listening nodes appear to be also exposing their RPC server to the public internet. To attempt to mitigate this: * Only ever bind localhost by default, even if `rpcallowip` is specified. (A warning is given if `rpcallowip` is specified without `rpcbind`, since it doesn't really make sense to do.) * Warn about exposing the RPC server to untrusted networks if the user explicitly binds to any INADDR_ANY address. * Include a warning about untrusted networks in the `--help` documentation for `rpcbind`. Tree-SHA512: 755bbca3db416a31393672eccf6675a5ee4d1eb1812cba73ebb4ff8c6b855ecc5df4c692566e9aa7b0f7d4dce6fedb9c0e9f3c265b9663aca36c4a6ba5efdbd4
1 parent cfeeba7 commit 9898400

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

src/httpserver.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,12 @@ static bool HTTPBindAddresses(struct evhttp* http)
300300
std::vector<std::pair<std::string, uint16_t> > endpoints;
301301

302302
// Determine what addresses to bind to
303-
if (!gArgs.IsArgSet("-rpcallowip")) { // Default to loopback if not allowing external IPs
303+
if (!(gArgs.IsArgSet("-rpcallowip") && gArgs.IsArgSet("-rpcbind"))) { // Default to loopback if not allowing external IPs
304304
endpoints.push_back(std::make_pair("::1", defaultPort));
305305
endpoints.push_back(std::make_pair("127.0.0.1", defaultPort));
306+
if (gArgs.IsArgSet("-rpcallowip")) {
307+
LogPrintf("WARNING: option -rpcallowip was specified without -rpcbind; this doesn't usually make sense\n");
308+
}
306309
if (gArgs.IsArgSet("-rpcbind")) {
307310
LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
308311
}
@@ -313,16 +316,17 @@ static bool HTTPBindAddresses(struct evhttp* http)
313316
SplitHostPort(strRPCBind, port, host);
314317
endpoints.push_back(std::make_pair(host, port));
315318
}
316-
} else { // No specific bind address specified, bind to any
317-
endpoints.push_back(std::make_pair("::", defaultPort));
318-
endpoints.push_back(std::make_pair("0.0.0.0", defaultPort));
319319
}
320320

321321
// Bind addresses
322322
for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) {
323323
LogPrint(BCLog::HTTP, "Binding RPC on address %s port %i\n", i->first, i->second);
324324
evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? nullptr : i->first.c_str(), i->second);
325325
if (bind_handle) {
326+
CNetAddr addr;
327+
if (i->first.empty() || (LookupHost(i->first.c_str(), addr, false) && addr.IsBindAny())) {
328+
LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n");
329+
}
326330
boundSockets.push_back(bind_handle);
327331
} else {
328332
LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second);

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ std::string HelpMessage(HelpMessageMode mode)
622622
strUsage += HelpMessageGroup(_("RPC server options:"));
623623
strUsage += HelpMessageOpt("-server", _("Accept command line and JSON-RPC commands"));
624624
strUsage += HelpMessageOpt("-rest", strprintf(_("Accept public REST requests (default: %u)"), DEFAULT_REST_ENABLE));
625-
strUsage += HelpMessageOpt("-rpcbind=<addr>[:port]", _("Bind to given address to listen for JSON-RPC connections. This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses)"));
625+
strUsage += HelpMessageOpt("-rpcbind=<addr>[:port]", _("Bind to given address to listen for JSON-RPC connections. Do not expose the RPC server to untrusted networks such as the public internet! This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses)"));
626626
strUsage += HelpMessageOpt("-rpccookiefile=<loc>", _("Location of the auth cookie (default: data dir)"));
627627
strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
628628
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));

src/netaddress.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ unsigned int CNetAddr::GetByte(int n) const
8686
return ip[15-n];
8787
}
8888

89+
bool CNetAddr::IsBindAny() const
90+
{
91+
const int cmplen = IsIPv4() ? 4 : 16;
92+
for (int i = 0; i < cmplen; ++i) {
93+
if (GetByte(i)) return false;
94+
}
95+
96+
return true;
97+
}
98+
8999
bool CNetAddr::IsIPv4() const
90100
{
91101
return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);

src/netaddress.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CNetAddr
5656
bool SetInternal(const std::string& name);
5757

5858
bool SetSpecial(const std::string &strName); // for Tor addresses
59+
bool IsBindAny() const; // INADDR_ANY equivalent
5960
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
6061
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
6162
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)

0 commit comments

Comments
 (0)