|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2015-2018 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 node responses to invalid network messages.""" |
| 6 | +import struct |
| 7 | + |
| 8 | +from test_framework import messages |
| 9 | +from test_framework.mininode import P2PDataStore |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | + |
| 12 | + |
| 13 | +class msg_unrecognized: |
| 14 | + """Nonsensical message. Modeled after similar types in test_framework.messages.""" |
| 15 | + |
| 16 | + command = b'badmsg' |
| 17 | + |
| 18 | + def __init__(self, str_data): |
| 19 | + self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data |
| 20 | + |
| 21 | + def serialize(self): |
| 22 | + return messages.ser_string(self.str_data) |
| 23 | + |
| 24 | + def __repr__(self): |
| 25 | + return "{}(data={})".format(self.command, self.str_data) |
| 26 | + |
| 27 | + |
| 28 | +class msg_nametoolong(msg_unrecognized): |
| 29 | + |
| 30 | + command = b'thisnameiswayyyyyyyyytoolong' |
| 31 | + |
| 32 | + |
| 33 | +class InvalidMessagesTest(BitcoinTestFramework): |
| 34 | + |
| 35 | + def set_test_params(self): |
| 36 | + self.num_nodes = 1 |
| 37 | + self.setup_clean_chain = True |
| 38 | + |
| 39 | + def run_test(self): |
| 40 | + """ |
| 41 | + 0. Send a bunch of large (4MB) messages of an unrecognized type. Check to see |
| 42 | + that it isn't an effective DoS against the node. |
| 43 | +
|
| 44 | + 1. Send an oversized (4MB+) message and check that we're disconnected. |
| 45 | +
|
| 46 | + 2. Send a few messages with an incorrect data size in the header, ensure the |
| 47 | + messages are ignored. |
| 48 | +
|
| 49 | + 3. Send an unrecognized message with a command name longer than 12 characters. |
| 50 | +
|
| 51 | + """ |
| 52 | + node = self.nodes[0] |
| 53 | + self.node = node |
| 54 | + node.add_p2p_connection(P2PDataStore()) |
| 55 | + conn2 = node.add_p2p_connection(P2PDataStore()) |
| 56 | + |
| 57 | + msg_limit = 4 * 1000 * 1000 # 4MB, per MAX_PROTOCOL_MESSAGE_LENGTH |
| 58 | + valid_data_limit = msg_limit - 5 # Account for the 4-byte length prefix |
| 59 | + |
| 60 | + # |
| 61 | + # 0. |
| 62 | + # |
| 63 | + # Send as large a message as is valid, ensure we aren't disconnected but |
| 64 | + # also can't exhaust resources. |
| 65 | + # |
| 66 | + msg_at_size = msg_unrecognized("b" * valid_data_limit) |
| 67 | + assert len(msg_at_size.serialize()) == msg_limit |
| 68 | + |
| 69 | + with node.assert_memory_usage_stable(perc_increase_allowed=0.03): |
| 70 | + self.log.info( |
| 71 | + "Sending a bunch of large, junk messages to test " |
| 72 | + "memory exhaustion. May take a bit...") |
| 73 | + |
| 74 | + # Run a bunch of times to test for memory exhaustion. |
| 75 | + for _ in range(200): |
| 76 | + node.p2p.send_message(msg_at_size) |
| 77 | + |
| 78 | + # Check that, even though the node is being hammered by nonsense from one |
| 79 | + # connection, it can still service other peers in a timely way. |
| 80 | + for _ in range(20): |
| 81 | + conn2.sync_with_ping(timeout=2) |
| 82 | + |
| 83 | + # Peer 1, despite serving up a bunch of nonsense, should still be connected. |
| 84 | + self.log.info("Waiting for node to drop junk messages.") |
| 85 | + node.p2p.sync_with_ping(timeout=8) |
| 86 | + assert node.p2p.is_connected |
| 87 | + |
| 88 | + # |
| 89 | + # 1. |
| 90 | + # |
| 91 | + # Send an oversized message, ensure we're disconnected. |
| 92 | + # |
| 93 | + msg_over_size = msg_unrecognized("b" * (valid_data_limit + 1)) |
| 94 | + assert len(msg_over_size.serialize()) == (msg_limit + 1) |
| 95 | + |
| 96 | + with node.assert_debug_log(["Oversized message from peer=0, disconnecting"]): |
| 97 | + # An unknown message type (or *any* message type) over |
| 98 | + # MAX_PROTOCOL_MESSAGE_LENGTH should result in a disconnect. |
| 99 | + node.p2p.send_message(msg_over_size) |
| 100 | + node.p2p.wait_for_disconnect(timeout=4) |
| 101 | + |
| 102 | + node.disconnect_p2ps() |
| 103 | + conn = node.add_p2p_connection(P2PDataStore()) |
| 104 | + conn.wait_for_verack() |
| 105 | + |
| 106 | + # |
| 107 | + # 2. |
| 108 | + # |
| 109 | + # Send messages with an incorrect data size in the header. |
| 110 | + # |
| 111 | + actual_size = 100 |
| 112 | + msg = msg_unrecognized("b" * actual_size) |
| 113 | + |
| 114 | + # TODO: handle larger-than cases. I haven't been able to pin down what behavior to expect. |
| 115 | + for wrong_size in (2, 77, 78, 79): |
| 116 | + self.log.info("Sending a message with incorrect size of {}".format(wrong_size)) |
| 117 | + |
| 118 | + # Unmodified message should submit okay. |
| 119 | + node.p2p.send_and_ping(msg) |
| 120 | + |
| 121 | + # A message lying about its data size results in a disconnect when the incorrect |
| 122 | + # data size is less than the actual size. |
| 123 | + # |
| 124 | + # TODO: why does behavior change at 78 bytes? |
| 125 | + # |
| 126 | + node.p2p.send_raw_message(self._tweak_msg_data_size(msg, wrong_size)) |
| 127 | + |
| 128 | + # For some reason unknown to me, we sometimes have to push additional data to the |
| 129 | + # peer in order for it to realize a disconnect. |
| 130 | + try: |
| 131 | + node.p2p.send_message(messages.msg_ping(nonce=123123)) |
| 132 | + except IOError: |
| 133 | + pass |
| 134 | + |
| 135 | + node.p2p.wait_for_disconnect(timeout=10) |
| 136 | + node.disconnect_p2ps() |
| 137 | + node.add_p2p_connection(P2PDataStore()) |
| 138 | + |
| 139 | + # |
| 140 | + # 3. |
| 141 | + # |
| 142 | + # Send a message with a too-long command name. |
| 143 | + # |
| 144 | + node.p2p.send_message(msg_nametoolong("foobar")) |
| 145 | + node.p2p.wait_for_disconnect(timeout=4) |
| 146 | + |
| 147 | + # Node is still up. |
| 148 | + conn = node.add_p2p_connection(P2PDataStore()) |
| 149 | + conn.sync_with_ping() |
| 150 | + |
| 151 | + |
| 152 | + def _tweak_msg_data_size(self, message, wrong_size): |
| 153 | + """ |
| 154 | + Return a raw message based on another message but with an incorrect data size in |
| 155 | + the message header. |
| 156 | + """ |
| 157 | + raw_msg = self.node.p2p.build_message(message) |
| 158 | + |
| 159 | + bad_size_bytes = struct.pack("<I", wrong_size) |
| 160 | + num_header_bytes_before_size = 4 + 12 |
| 161 | + |
| 162 | + # Replace the correct data size in the message with an incorrect one. |
| 163 | + raw_msg_with_wrong_size = ( |
| 164 | + raw_msg[:num_header_bytes_before_size] + |
| 165 | + bad_size_bytes + |
| 166 | + raw_msg[(num_header_bytes_before_size + len(bad_size_bytes)):] |
| 167 | + ) |
| 168 | + assert len(raw_msg) == len(raw_msg_with_wrong_size) |
| 169 | + |
| 170 | + return raw_msg_with_wrong_size |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == '__main__': |
| 175 | + InvalidMessagesTest().main() |
0 commit comments