Skip to content

Commit d3e6247

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 4976e91 commit d3e6247

File tree

5 files changed

+156
-29
lines changed

5 files changed

+156
-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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2014-2019 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+
test_ipv6_local,
16+
)
17+
from test_framework.test_framework import (
18+
BitcoinTestFramework,
19+
SkipTest,
20+
)
21+
from test_framework.util import (
22+
PORT_MIN,
23+
PORT_RANGE,
24+
assert_equal,
25+
p2p_port,
26+
rpc_port,
27+
)
28+
29+
# From chainparamsbase.cpp:CreateBaseChainParams().
30+
REGTEST_TOR_TARGET_PORT = 18445
31+
32+
class BindExtraTest(BitcoinTestFramework):
33+
def set_test_params(self):
34+
# Avoid any -bind= on the command line. Force the framework to avoid
35+
# adding -bind=127.0.0.1.
36+
self.setup_clean_chain = True
37+
self.bind_to_localhost_only = False
38+
self.num_nodes = 4
39+
40+
def setup_network(self):
41+
# Override setup_network() because we want to put the result of
42+
# p2p_port() in self.extra_args[], before the nodes are started.
43+
# p2p_port() is not usable in set_test_params() because PortSeed.n is
44+
# not set at that time.
45+
46+
# Due to OS-specific network stats queries, we only run on Linux.
47+
self.log.info("Checking for Linux")
48+
if not sys.platform.startswith('linux'):
49+
raise SkipTest("This test can only be run on Linux.")
50+
51+
self.have_ipv6 = test_ipv6_local()
52+
53+
any_ipv4 = addr_to_hex('0.0.0.0')
54+
any_ipv6 = addr_to_hex('::')
55+
loopback_ipv4 = addr_to_hex('127.0.0.1')
56+
loopback_ipv6 = addr_to_hex('::1')
57+
58+
# Start custom ports after p2p and rpc ports.
59+
port = PORT_MIN + 2 * PORT_RANGE
60+
61+
# Array of tuples [command line arguments, expected bind addresses].
62+
self.expected = []
63+
64+
# Node0, no -bind, expected to bind on any + tor target.
65+
self.expected.append(
66+
[
67+
[],
68+
[(any_ipv4, p2p_port(0)), (loopback_ipv4, REGTEST_TOR_TARGET_PORT)]
69+
]
70+
)
71+
if self.have_ipv6:
72+
self.expected[0][1].append((any_ipv6, p2p_port(0)))
73+
74+
# Node1, no -bind=...=onion, thus no extra port for Tor target.
75+
self.expected.append(
76+
[
77+
['-bind=127.0.0.1:{}'.format(port)],
78+
[(loopback_ipv4, port)]
79+
],
80+
)
81+
port += 1
82+
83+
# Node2, no normal -bind, thus only the Tor target.
84+
self.expected.append(
85+
[
86+
['-bind=127.0.0.1:{}=onion'.format(port)],
87+
[(loopback_ipv4, port)]
88+
],
89+
)
90+
port += 1
91+
92+
# Node3, both -bind and -bind=...=onion.
93+
self.expected.append(
94+
[
95+
['-bind=127.0.0.1:{}'.format(port), '-bind=127.0.0.1:{}=onion'.format(port + 1)],
96+
[(loopback_ipv4, port), (loopback_ipv4, port + 1)]
97+
],
98+
)
99+
port += 2
100+
101+
# Add RPC ports to the list of expected ports to bind to for all nodes.
102+
# They are not relevant for this test.
103+
for i in range(len(self.expected)):
104+
self.expected[i][1].append((loopback_ipv4, rpc_port(i)))
105+
if self.have_ipv6:
106+
self.expected[i][1].append((loopback_ipv6, rpc_port(i)))
107+
108+
self.extra_args = list(map(lambda e: e[0], self.expected))
109+
self.setup_nodes()
110+
111+
def run_test(self):
112+
for i in range(len(self.expected)):
113+
pid = self.nodes[i].process.pid
114+
assert_equal(set(get_bind_addrs(pid)), set(self.expected[i][1]))
115+
116+
if __name__ == '__main__':
117+
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)