Skip to content

Commit c055a50

Browse files
committed
net: don't extra bind for Tor if binds are restricted
The semantic of `-bind` is to restrict the binding only to some address. If not specified, then the user does not care and we bind to `0.0.0.0`. If specified then we should honor the restriction and bind only to the specified address. Before this change, if no `-bind` is given then we would bind to `0.0.0.0` and to `127.0.0.1:8334` (incoming Tor) which is ok - the user does not care to restrict the binding. However: * If only `-bind=addr` is given (without `-bind=...=onion`) then we would bind to `addr` _and_ to `127.0.0.1:8334` in addition which may be unexpected assuming the perception of `-bind=addr` is "bind _only_ to `addr`". * If only `-bind=addr=onion` is given (without ordinary `-bind=`) then we would bind to `addr` _and_ to `0.0.0.0` in addition. Change the above two cases to not do the additional bind. If `-bind` is given then only bind to the specified address: * If only `-bind=addr` is given (without `-bind=...=onion`) then bind to `addr` (only). If we are creating tor hidden service then use `addr` as target. Same behavior as before bitcoin#19991. * If only `-bind=addr=onion` is given (without ordinary `-bind=`) then bind to `addr` (only) and consider incoming connections as Tor and do not advertise `addr`. I.e. a Tor-only node.
1 parent d67883d commit c055a50

File tree

5 files changed

+153
-29
lines changed

5 files changed

+153
-29
lines changed

src/init.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,25 +1965,39 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19651965
return InitError(ResolveErrMsg("bind", bind_arg));
19661966
}
19671967

1968-
if (connOptions.onion_binds.empty()) {
1969-
connOptions.onion_binds.push_back(DefaultOnionServiceTarget());
1970-
}
1971-
1972-
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)) {
1973-
const auto bind_addr = connOptions.onion_binds.front();
1974-
if (connOptions.onion_binds.size() > 1) {
1975-
InitWarning(strprintf(_("More than one onion bind address is provided. Using %s for the automatically created Tor onion service."), bind_addr.ToStringIPPort()));
1976-
}
1977-
StartTorControl(bind_addr);
1978-
}
1979-
19801968
for (const std::string& strBind : args.GetArgs("-whitebind")) {
19811969
NetWhitebindPermissions whitebind;
19821970
bilingual_str error;
19831971
if (!NetWhitebindPermissions::TryParse(strBind, whitebind, error)) return InitError(error);
19841972
connOptions.vWhiteBinds.push_back(whitebind);
19851973
}
19861974

1975+
// If the user did not specify -bind= or -whitebind= then we bind
1976+
// on any address - 0.0.0.0 (IPv4) and :: (IPv6).
1977+
connOptions.bind_on_any = args.GetArgs("-bind").empty() && args.GetArgs("-whitebind").empty();
1978+
1979+
CService onion_service_target;
1980+
if (!connOptions.onion_binds.empty()) {
1981+
onion_service_target = connOptions.onion_binds.front();
1982+
} else if (!connOptions.vBinds.empty()) {
1983+
onion_service_target = connOptions.vBinds.front();
1984+
} else if (!connOptions.vWhiteBinds.empty()) {
1985+
onion_service_target = connOptions.vWhiteBinds.front().m_service;
1986+
} else {
1987+
assert(connOptions.bind_on_any);
1988+
onion_service_target = DefaultOnionServiceTarget();
1989+
connOptions.onion_binds.push_back(onion_service_target);
1990+
}
1991+
1992+
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)) {
1993+
if (connOptions.onion_binds.size() > 1) {
1994+
InitWarning(strprintf(_("More than one onion bind address is provided. Using %s "
1995+
"for the automatically created Tor onion service."),
1996+
onion_service_target.ToStringIPPort()));
1997+
}
1998+
StartTorControl(onion_service_target);
1999+
}
2000+
19872001
for (const auto& net : args.GetArgs("-whitelist")) {
19882002
NetWhitelistPermissions subnet;
19892003
bilingual_str error;

src/net.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,30 +2391,25 @@ bool CConnman::Bind(const CService &addr, unsigned int flags, NetPermissionFlags
23912391
return true;
23922392
}
23932393

2394-
bool CConnman::InitBinds(
2395-
const std::vector<CService>& binds,
2396-
const std::vector<NetWhitebindPermissions>& whiteBinds,
2397-
const std::vector<CService>& onion_binds)
2394+
bool CConnman::InitBinds(const Options& options)
23982395
{
23992396
bool fBound = false;
2400-
for (const auto& addrBind : binds) {
2397+
for (const auto& addrBind : options.vBinds) {
24012398
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR), NetPermissionFlags::PF_NONE);
24022399
}
2403-
for (const auto& addrBind : whiteBinds) {
2400+
for (const auto& addrBind : options.vWhiteBinds) {
24042401
fBound |= Bind(addrBind.m_service, (BF_EXPLICIT | BF_REPORT_ERROR), addrBind.m_flags);
24052402
}
2406-
if (binds.empty() && whiteBinds.empty()) {
2403+
for (const auto& addr_bind : options.onion_binds) {
2404+
fBound |= Bind(addr_bind, BF_EXPLICIT | BF_REPORT_ERROR | BF_DONT_ADVERTISE, NetPermissionFlags::PF_NONE);
2405+
}
2406+
if (options.bind_on_any) {
24072407
struct in_addr inaddr_any;
24082408
inaddr_any.s_addr = htonl(INADDR_ANY);
24092409
struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
24102410
fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE, NetPermissionFlags::PF_NONE);
24112411
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE, NetPermissionFlags::PF_NONE);
24122412
}
2413-
2414-
for (const auto& addr_bind : onion_binds) {
2415-
fBound |= Bind(addr_bind, BF_EXPLICIT | BF_DONT_ADVERTISE, NetPermissionFlags::PF_NONE);
2416-
}
2417-
24182413
return fBound;
24192414
}
24202415

@@ -2433,7 +2428,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
24332428
nMaxOutboundCycleStartTime = 0;
24342429
}
24352430

2436-
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds, connOptions.onion_binds)) {
2431+
if (fListen && !InitBinds(connOptions)) {
24372432
if (clientInterface) {
24382433
clientInterface->ThreadSafeMessageBox(
24392434
_("Failed to listen on any port. Use -listen=0 if you want this."),

src/net.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ class CConnman
217217
std::vector<NetWhitebindPermissions> vWhiteBinds;
218218
std::vector<CService> vBinds;
219219
std::vector<CService> onion_binds;
220+
/// True if the user did not specify -bind= or -whitebind= and thus
221+
/// we should bind on `0.0.0.0` (IPv4) and `::` (IPv6).
222+
bool bind_on_any;
220223
bool m_use_addrman_outgoing = true;
221224
std::vector<std::string> m_specified_outgoing;
222225
std::vector<std::string> m_added_nodes;
@@ -415,10 +418,7 @@ class CConnman
415418

416419
bool BindListenPort(const CService& bindAddr, bilingual_str& strError, NetPermissionFlags permissions);
417420
bool Bind(const CService& addr, unsigned int flags, NetPermissionFlags permissions);
418-
bool InitBinds(
419-
const std::vector<CService>& binds,
420-
const std::vector<NetWhitebindPermissions>& whiteBinds,
421-
const std::vector<CService>& onion_binds);
421+
bool InitBinds(const Options& options);
422422

423423
void ThreadOpenAddedConnections();
424424
void AddAddrFetch(const std::string& strDest);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2014-2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""
6+
Test starting bitcoind with -bind and/or -bind=...=onion and confirm
7+
that bind happens on the expected ports.
8+
"""
9+
10+
import sys
11+
12+
from test_framework.netutil import (
13+
addr_to_hex,
14+
get_bind_addrs,
15+
)
16+
from test_framework.test_framework import (
17+
BitcoinTestFramework,
18+
SkipTest,
19+
)
20+
from test_framework.util import (
21+
PORT_MIN,
22+
PORT_RANGE,
23+
assert_equal,
24+
p2p_port,
25+
rpc_port,
26+
)
27+
28+
# From chainparamsbase.cpp:CreateBaseChainParams().
29+
REGTEST_TOR_TARGET_PORT = 18445
30+
31+
class BindExtraTest(BitcoinTestFramework):
32+
def set_test_params(self):
33+
# Avoid any -bind= on the command line. Force the framework to avoid
34+
# adding -bind=127.0.0.1.
35+
self.setup_clean_chain = True
36+
self.bind_to_localhost_only = False
37+
self.num_nodes = 4
38+
39+
def setup_network(self):
40+
# Override setup_network() because we want to put the result of
41+
# p2p_port() in self.extra_args[], before the nodes are started.
42+
# p2p_port() is not usable in set_test_params() because PortSeed.n is
43+
# not set at that time.
44+
45+
# Due to OS-specific network stats queries, we only run on Linux.
46+
self.log.info("Checking for Linux")
47+
if not sys.platform.startswith('linux'):
48+
raise SkipTest("This test can only be run on Linux.")
49+
50+
any_ipv4 = addr_to_hex('0.0.0.0')
51+
loopback_ipv4 = addr_to_hex('127.0.0.1')
52+
53+
# Start custom ports after p2p and rpc ports.
54+
port = PORT_MIN + 2 * PORT_RANGE
55+
56+
# Array of tuples [command line arguments, expected bind addresses].
57+
self.expected = []
58+
59+
# Node0, no -bind, expected to bind on any + tor target.
60+
self.expected.append(
61+
[
62+
[],
63+
[(any_ipv4, p2p_port(0)), (loopback_ipv4, REGTEST_TOR_TARGET_PORT)]
64+
]
65+
)
66+
67+
# Node1, no -bind=...=onion, thus no extra port for Tor target.
68+
self.expected.append(
69+
[
70+
['-bind=127.0.0.1:{}'.format(port)],
71+
[(loopback_ipv4, port)]
72+
],
73+
)
74+
port += 1
75+
76+
# Node2, no normal -bind, thus only the Tor target.
77+
self.expected.append(
78+
[
79+
['-bind=127.0.0.1:{}=onion'.format(port)],
80+
[(loopback_ipv4, port)]
81+
],
82+
)
83+
port += 1
84+
85+
# Node3, both -bind and -bind=...=onion.
86+
self.expected.append(
87+
[
88+
['-bind=127.0.0.1:{}'.format(port), '-bind=127.0.0.1:{}=onion'.format(port + 1)],
89+
[(loopback_ipv4, port), (loopback_ipv4, port + 1)]
90+
],
91+
)
92+
port += 2
93+
94+
# Add RPC ports to the list of expected ports to bind to for all nodes.
95+
# They are not relevant for this test.
96+
for i in range(len(self.expected)):
97+
self.expected[i][1].append((loopback_ipv4, rpc_port(i)))
98+
99+
self.extra_args = list(map(lambda e: e[0], self.expected))
100+
self.setup_nodes()
101+
102+
def run_test(self):
103+
for i in range(len(self.expected)):
104+
pid = self.nodes[i].process.pid
105+
actual = set(get_bind_addrs(pid))
106+
# Remove IPv6 addresses because on some CI environments "::1" is not configured
107+
# on the system (so our test_ipv6_local() would return False), but it is
108+
# possible to bind on "::". This makes it unpredictable whether to expect
109+
# that bitcoind has bound on "::1" (for RPC) and "::" (for P2P).
110+
actual_without_ipv6 = set(filter(lambda e: len(e[0]) != 32, actual))
111+
assert_equal(actual_without_ipv6, set(self.expected[i][1]))
112+
113+
if __name__ == '__main__':
114+
BindExtraTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
'feature_fee_estimation.py',
130130
'interface_zmq.py',
131131
'interface_bitcoin_cli.py',
132+
'feature_bind_extra.py',
132133
'mempool_resurrect.py',
133134
'wallet_txn_doublespend.py --mineblock',
134135
'tool_wallet.py',

0 commit comments

Comments
 (0)