Skip to content

Commit da4cbb7

Browse files
author
MarcoFalke
committed
Merge #18544: net: limit BIP37 filter lifespan (active between 'filterload'..'filterclear')
a9ecbdf test: add more inactive filter tests to p2p_filter.py (Sebastian Falbesoner) 5eae034 net: limit BIP37 filter lifespan (active between 'filterload' and 'filterclear') (Sebastian Falbesoner) Pull request description: This PR fixes bitcoin/bitcoin#18483. On the master branch, there is currently _always_ a BIP37 filter set for every peer: if not a specific filter is set through a `filterload` message, a default match-everything filter is instanciated and pointed to via the `CBloomFilter` default constructor; that happens both initially, when the containing structure `TxRelay` is constructed: https://github.com/bitcoin/bitcoin/blob/c0b389b33516fb3eaaad7c30bd11dba768882a7e/src/net.h#L812 and after a loaded filter is removed again through a `filterclear` message: https://github.com/bitcoin/bitcoin/blob/c0b389b33516fb3eaaad7c30bd11dba768882a7e/src/net_processing.cpp#L3201 The behaviour was introduced by commit bitcoin/bitcoin@37c6389 (an intentional covert fix for [CVE-2013-5700](bitcoin/bitcoin#18515), according to gmaxwell). This default match-everything filter leads to some unintended side-effects: 1. `getdata` request for filtered blocks (i.e. type `MSG_FILTERED_BLOCK`) are always responded to with `merkleblock`s, even if no filter was set by the peer, see issue #18483 (strictly speaking, this is a violation of BIP37) https://github.com/bitcoin/bitcoin/blob/c0b389b33516fb3eaaad7c30bd11dba768882a7e/src/net_processing.cpp#L1504-L1507 2. if a peer sends a `filteradd` message without having loaded a filter via `filterload` before, the intended increasing of the banscore never happens (triggered if `bad` is set to true, a few lines below) https://github.com/bitcoin/bitcoin/blob/c0b389b33516fb3eaaad7c30bd11dba768882a7e/src/net_processing.cpp#L3182-L3186 This PR basically activates the `else`-branch code paths for all checks of `pfilter` again (on the master branch, they are dead code) by limiting the pointer's lifespan: instead of always having a filter set, the `pfilter` is only pointing to a `CBloomFilter`-instance after receiving a `filterload` message and the instance is destroyed again (and the pointer nullified) after receiving a `filterclear` message. Here is a before/after comparison in behaviour: | code part / scenario | master branch | PR branch | | --------------------------------------------- | ---------------------------------- | ---------------------------------------------------- | | `getdata` processing for `MSG_FILTERED_BLOCK` | always responds with `merkleblock` | only responds if filter was set via `filterload` | | `filteradd` processing, no filter was loaded | nothing | peer's banscore increases by 100 (i.e. disconnect) | On the other code parts where `pfilter` is checked there is no change in the logic behaviour (except that `CBloomFilter::IsRelevantAndUpdate()` is unnecessarily called and immediately returned in the master branch). Note that the default constructor of `CBloomFilter` is only used for deserializing the received `filterload` message and nowhere else. The PR also contains a functional test checking that sending `getdata` for filtered blocks is ignored by the node if no bloom filter is set. ACKs for top commit: MarcoFalke: re-ACK a9ecbdf, only change is in test code 🕙 Tree-SHA512: 1a656a6d74ccaf628e7fdca063ba63fbab2089e0b6d0a11be9bbd387c2ee6d3230706ff8ffc1a55711481df3d4547137dd7c9d9184d89eaa43ade4927792d0b6
2 parents dc5da7f + a9ecbdf commit da4cbb7

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/net.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,14 +809,13 @@ class CNode
809809
RecursiveMutex cs_inventory;
810810

811811
struct TxRelay {
812-
TxRelay() { pfilter = MakeUnique<CBloomFilter>(); }
813812
mutable RecursiveMutex cs_filter;
814813
// We use fRelayTxes for two purposes -
815814
// a) it allows us to not relay tx invs before receiving the peer's version message
816815
// b) the peer may tell us in its version message that we should not relay tx invs
817816
// unless it loads a bloom filter.
818817
bool fRelayTxes GUARDED_BY(cs_filter){false};
819-
std::unique_ptr<CBloomFilter> pfilter PT_GUARDED_BY(cs_filter) GUARDED_BY(cs_filter);
818+
std::unique_ptr<CBloomFilter> pfilter PT_GUARDED_BY(cs_filter) GUARDED_BY(cs_filter){nullptr};
820819

821820
mutable RecursiveMutex cs_tx_inventory;
822821
CRollingBloomFilter filterInventoryKnown GUARDED_BY(cs_tx_inventory){50000, 0.000001};

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3198,7 +3198,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
31983198
}
31993199
LOCK(pfrom->m_tx_relay->cs_filter);
32003200
if (pfrom->GetLocalServices() & NODE_BLOOM) {
3201-
pfrom->m_tx_relay->pfilter.reset(new CBloomFilter());
3201+
pfrom->m_tx_relay->pfilter = nullptr;
32023202
}
32033203
pfrom->m_tx_relay->fRelayTxes = true;
32043204
return true;

test/functional/p2p_filter.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
"""
88

99
from test_framework.messages import (
10+
CInv,
1011
MSG_BLOCK,
1112
MSG_FILTERED_BLOCK,
12-
msg_getdata,
13-
msg_filterload,
1413
msg_filteradd,
1514
msg_filterclear,
15+
msg_filterload,
16+
msg_getdata,
1617
)
1718
from test_framework.mininode import P2PInterface
1819
from test_framework.test_framework import BitcoinTestFramework
20+
from test_framework.util import assert_equal
1921

2022

2123
class FilterNode(P2PInterface):
@@ -35,8 +37,9 @@ def on_inv(self, message):
3537
for i in message.inv:
3638
# inv messages can only contain TX or BLOCK, so translate BLOCK to FILTERED_BLOCK
3739
if i.type == MSG_BLOCK:
38-
i.type = MSG_FILTERED_BLOCK
39-
want.inv.append(i)
40+
want.inv.append(CInv(MSG_FILTERED_BLOCK, i.hash))
41+
else:
42+
want.inv.append(i)
4043
if len(want.inv):
4144
self.send_message(want)
4245

@@ -103,6 +106,21 @@ def run_test(self):
103106
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 7)
104107
filter_node.wait_for_tx(txid)
105108

109+
self.log.info('Check that request for filtered blocks is ignored if no filter is set')
110+
filter_node.merkleblock_received = False
111+
filter_node.tx_received = False
112+
with self.nodes[0].assert_debug_log(expected_msgs=['received getdata']):
113+
block_hash = self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())[0]
114+
filter_node.wait_for_inv([CInv(MSG_BLOCK, int(block_hash, 16))])
115+
filter_node.sync_with_ping()
116+
assert not filter_node.merkleblock_received
117+
assert not filter_node.tx_received
118+
119+
self.log.info('Check that sending "filteradd" if no filter is set is treated as misbehavior (+100)')
120+
assert_equal(self.nodes[0].getpeerinfo()[0]['banscore'], 0)
121+
filter_node.send_and_ping(msg_filteradd(data=b'letsmisbehave'))
122+
assert_equal(self.nodes[0].getpeerinfo()[0]['banscore'], 100)
123+
106124
self.log.info("Check that division-by-zero remote crash bug [CVE-2013-5700] is fixed")
107125
filter_node.send_and_ping(msg_filterload(data=b'', nHashFuncs=1))
108126
filter_node.send_and_ping(msg_filteradd(data=b'letstrytocrashthisnode'))

0 commit comments

Comments
 (0)