Skip to content

Commit b80cdfe

Browse files
author
MarcoFalke
committed
Merge #16618: [Fix] Allow connection of a noban banned peer
d117f45 Add test for setban (nicolas.dorier) dc7529a [Fix] Allow connection of a noban banned peer (nicolas.dorier) Pull request description: Reported by @MarcoFalke on bitcoin/bitcoin#16248 (comment) The bug would mean that if the peer connecting to you is banned, but whitelisted without specific permissions, it would not be able to connect to the node. The solution is just to move the same line below. ACKs for top commit: Sjors: Agree inline is more clear. utACK d117f45 MarcoFalke: ACK d117f45 Tree-SHA512: 0fed39acb1e8db67bb0bf4c4de3ad034ae776f38d55bd661f1ae0e1a4c6becaf1824ab46ed8279f2f31df3f4b29ff56461d8b167d3e9cece62cfe58b5a912811
2 parents aed15ed + d117f45 commit b80cdfe

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,6 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
906906
NetPermissionFlags permissionFlags = NetPermissionFlags::PF_NONE;
907907
hListenSocket.AddSocketPermissionFlags(permissionFlags);
908908
AddWhitelistPermissionFlags(permissionFlags, addr);
909-
const bool noban = NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::PF_NOBAN);
910909
bool legacyWhitelisted = false;
911910
if (NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::PF_ISIMPLICIT)) {
912911
NetPermissions::ClearFlag(permissionFlags, PF_ISIMPLICIT);
@@ -953,7 +952,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
953952

954953
// Don't accept connections from banned peers, but if our inbound slots aren't almost full, accept
955954
// if the only banning reason was an automatic misbehavior ban.
956-
if (!noban && bannedlevel > ((nInbound + 1 < nMaxInbound) ? 1 : 0))
955+
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::PF_NOBAN) && bannedlevel > ((nInbound + 1 < nMaxInbound) ? 1 : 0))
957956
{
958957
LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString());
959958
CloseSocket(hSocket);

test/functional/rpc_setban.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2015-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+
"""Test the setban rpc call."""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import (
9+
connect_nodes,
10+
p2p_port
11+
)
12+
13+
class SetBanTests(BitcoinTestFramework):
14+
def set_test_params(self):
15+
self.num_nodes = 2
16+
self.setup_clean_chain = True
17+
self.extra_args = [[],[]]
18+
19+
def run_test(self):
20+
# Node 0 connects to Node 1, check that the noban permission is not granted
21+
connect_nodes(self.nodes[0], 1)
22+
peerinfo = self.nodes[1].getpeerinfo()[0]
23+
assert(not 'noban' in peerinfo['permissions'])
24+
25+
# Node 0 get banned by Node 1
26+
self.nodes[1].setban("127.0.0.1", "add")
27+
28+
# Node 0 should not be able to reconnect
29+
with self.nodes[1].assert_debug_log(expected_msgs=['dropped (banned)\n']):
30+
self.restart_node(1, [])
31+
self.nodes[0].addnode("127.0.0.1:" + str(p2p_port(1)), "onetry")
32+
33+
# However, node 0 should be able to reconnect if it has noban permission
34+
self.restart_node(1, ['-whitelist=127.0.0.1'])
35+
connect_nodes(self.nodes[0], 1)
36+
peerinfo = self.nodes[1].getpeerinfo()[0]
37+
assert('noban' in peerinfo['permissions'])
38+
39+
# If we remove the ban, Node 0 should be able to reconnect even without noban permission
40+
self.nodes[1].setban("127.0.0.1", "remove")
41+
self.restart_node(1, [])
42+
connect_nodes(self.nodes[0], 1)
43+
peerinfo = self.nodes[1].getpeerinfo()[0]
44+
assert(not 'noban' in peerinfo['permissions'])
45+
46+
if __name__ == '__main__':
47+
SetBanTests().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
'rpc_net.py',
147147
'wallet_keypool.py',
148148
'p2p_mempool.py',
149+
'rpc_setban.py',
149150
'p2p_blocksonly.py',
150151
'mining_prioritisetransaction.py',
151152
'p2p_invalid_locator.py',

test/lint/lint-spelling.ignore-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ cachable
1212
errorstring
1313
keyserver
1414
homogenous
15+
setban

0 commit comments

Comments
 (0)