Skip to content

Commit 36aeb43

Browse files
committed
Merge #15246: qa: Add tests for invalid message headers
fa3745b qa: Add tests for invalid message headers (MarcoFalke) Pull request description: Tree-SHA512: b37e297cfd65a33a7af201f750a303cf437b438e40d38b1d2f562ccde67082616daa110ca1e5e3af6514ea4ca4b115362acf2ffa6263cea3c8e8189ce02dda67
2 parents a0d657b + fa3745b commit 36aeb43

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class msg_unrecognized:
1616

1717
command = b'badmsg'
1818

19-
def __init__(self, str_data):
19+
def __init__(self, *, str_data):
2020
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data
2121

2222
def serialize(self):
@@ -26,30 +26,27 @@ def __repr__(self):
2626
return "{}(data={})".format(self.command, self.str_data)
2727

2828

29-
class msg_nametoolong(msg_unrecognized):
30-
31-
command = b'thisnameiswayyyyyyyyytoolong'
32-
33-
3429
class InvalidMessagesTest(BitcoinTestFramework):
35-
3630
def set_test_params(self):
3731
self.num_nodes = 1
3832
self.setup_clean_chain = True
3933

4034
def run_test(self):
4135
"""
36+
. Test msg header
4237
0. Send a bunch of large (4MB) messages of an unrecognized type. Check to see
4338
that it isn't an effective DoS against the node.
4439
4540
1. Send an oversized (4MB+) message and check that we're disconnected.
4641
4742
2. Send a few messages with an incorrect data size in the header, ensure the
4843
messages are ignored.
49-
50-
3. Send an unrecognized message with a command name longer than 12 characters.
51-
5244
"""
45+
self.test_magic_bytes()
46+
self.test_checksum()
47+
self.test_size()
48+
self.test_command()
49+
5350
node = self.nodes[0]
5451
self.node = node
5552
node.add_p2p_connection(P2PDataStore())
@@ -64,7 +61,7 @@ def run_test(self):
6461
# Send as large a message as is valid, ensure we aren't disconnected but
6562
# also can't exhaust resources.
6663
#
67-
msg_at_size = msg_unrecognized("b" * valid_data_limit)
64+
msg_at_size = msg_unrecognized(str_data="b" * valid_data_limit)
6865
assert len(msg_at_size.serialize()) == msg_limit
6966

7067
increase_allowed = 0.5
@@ -94,10 +91,10 @@ def run_test(self):
9491
#
9592
# Send an oversized message, ensure we're disconnected.
9693
#
97-
msg_over_size = msg_unrecognized("b" * (valid_data_limit + 1))
94+
msg_over_size = msg_unrecognized(str_data="b" * (valid_data_limit + 1))
9895
assert len(msg_over_size.serialize()) == (msg_limit + 1)
9996

100-
with node.assert_debug_log(["Oversized message from peer=0, disconnecting"]):
97+
with node.assert_debug_log(["Oversized message from peer=4, disconnecting"]):
10198
# An unknown message type (or *any* message type) over
10299
# MAX_PROTOCOL_MESSAGE_LENGTH should result in a disconnect.
103100
node.p2p.send_message(msg_over_size)
@@ -113,7 +110,7 @@ def run_test(self):
113110
# Send messages with an incorrect data size in the header.
114111
#
115112
actual_size = 100
116-
msg = msg_unrecognized("b" * actual_size)
113+
msg = msg_unrecognized(str_data="b" * actual_size)
117114

118115
# TODO: handle larger-than cases. I haven't been able to pin down what behavior to expect.
119116
for wrong_size in (2, 77, 78, 79):
@@ -140,18 +137,58 @@ def run_test(self):
140137
node.disconnect_p2ps()
141138
node.add_p2p_connection(P2PDataStore())
142139

143-
#
144-
# 3.
145-
#
146-
# Send a message with a too-long command name.
147-
#
148-
node.p2p.send_message(msg_nametoolong("foobar"))
149-
node.p2p.wait_for_disconnect(timeout=4)
150-
151140
# Node is still up.
152141
conn = node.add_p2p_connection(P2PDataStore())
153142
conn.sync_with_ping()
154143

144+
def test_magic_bytes(self):
145+
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
146+
conn.magic_bytes = b'\x00\x11\x22\x32'
147+
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']):
148+
conn.send_message(messages.msg_ping(nonce=0xff))
149+
conn.wait_for_disconnect(timeout=1)
150+
self.nodes[0].disconnect_p2ps()
151+
152+
def test_checksum(self):
153+
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
154+
with self.nodes[0].assert_debug_log(['ProcessMessages(badmsg, 2 bytes): CHECKSUM ERROR expected 78df0a04 was ffffffff']):
155+
msg = conn.build_message(msg_unrecognized(str_data="d"))
156+
cut_len = (
157+
4 + # magic
158+
12 + # command
159+
4 #len
160+
)
161+
# modify checksum
162+
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
163+
self.nodes[0].p2p.send_raw_message(msg)
164+
conn.sync_with_ping(timeout=1)
165+
self.nodes[0].disconnect_p2ps()
166+
167+
def test_size(self):
168+
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
169+
with self.nodes[0].assert_debug_log(['']):
170+
msg = conn.build_message(msg_unrecognized(str_data="d"))
171+
cut_len = (
172+
4 + # magic
173+
12 # command
174+
)
175+
# modify len to MAX_SIZE + 1
176+
msg = msg[:cut_len] + struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:]
177+
self.nodes[0].p2p.send_raw_message(msg)
178+
conn.wait_for_disconnect(timeout=1)
179+
self.nodes[0].disconnect_p2ps()
180+
181+
def test_command(self):
182+
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
183+
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: ERRORS IN HEADER']):
184+
msg = msg_unrecognized(str_data="d")
185+
msg.command = b'\xff' * 12
186+
msg = conn.build_message(msg)
187+
# Modify command
188+
msg = msg[:7] + b'\x00' + msg[7 + 1:]
189+
self.nodes[0].p2p.send_raw_message(msg)
190+
conn.sync_with_ping(timeout=1)
191+
self.nodes[0].disconnect_p2ps()
155192

156193
def _tweak_msg_data_size(self, message, wrong_size):
157194
"""

test/functional/test_framework/mininode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def peer_connect(self, dstaddr, dstport, net="regtest"):
118118
# The initial message to send after the connection was made:
119119
self.on_connection_send_msg = None
120120
self.recvbuf = b""
121-
self.network = net
121+
self.magic_bytes = MAGIC_BYTES[net]
122122
logger.debug('Connecting to Bitcoin Node: %s:%d' % (self.dstaddr, self.dstport))
123123

124124
loop = NetworkThread.network_event_loop
@@ -170,7 +170,7 @@ def _on_data(self):
170170
while True:
171171
if len(self.recvbuf) < 4:
172172
return
173-
if self.recvbuf[:4] != MAGIC_BYTES[self.network]:
173+
if self.recvbuf[:4] != self.magic_bytes:
174174
raise ValueError("got garbage %s" % repr(self.recvbuf))
175175
if len(self.recvbuf) < 4 + 12 + 4 + 4:
176176
return
@@ -232,7 +232,7 @@ def build_message(self, message):
232232
"""Build a serialized P2P message"""
233233
command = message.command
234234
data = message.serialize()
235-
tmsg = MAGIC_BYTES[self.network]
235+
tmsg = self.magic_bytes
236236
tmsg += command
237237
tmsg += b"\x00" * (12 - len(command))
238238
tmsg += struct.pack("<I", len(data))

0 commit comments

Comments
 (0)