Skip to content

Commit 955eee7

Browse files
committed
net: Sanitize message type for logging
- Use `SanitizeString` when logging message errors to make sure that the message type is sanitized. - For the `MESSAGESTART` error don't inspect and log header details at all: receiving invalid start bytes makes it likely that the packet isn't even formatted as valid P2P message. Logging the four unexpected start bytes should be enough. - Update `p2p_invalid_messages.py` test to check this. Issue reported by gmaxwell.
1 parent 9c05da4 commit 955eee7

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/net.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,13 @@ int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
687687

688688
// Check start string, network magic
689689
if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
690-
LogPrint(BCLog::NET, "HEADER ERROR - MESSAGESTART (%s, %u bytes), received %s, peer=%d\n", hdr.GetCommand(), hdr.nMessageSize, HexStr(hdr.pchMessageStart), m_node_id);
690+
LogPrint(BCLog::NET, "HEADER ERROR - MESSAGESTART received %s, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
691691
return -1;
692692
}
693693

694694
// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
695695
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
696-
LogPrint(BCLog::NET, "HEADER ERROR - SIZE (%s, %u bytes), peer=%d\n", hdr.GetCommand(), hdr.nMessageSize, m_node_id);
696+
LogPrint(BCLog::NET, "HEADER ERROR - SIZE (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetCommand()), hdr.nMessageSize, m_node_id);
697697
return -1;
698698
}
699699

@@ -755,7 +755,7 @@ std::optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono
755755
msg = std::nullopt;
756756
} else if (!hdr.IsCommandValid()) {
757757
LogPrint(BCLog::NET, "HEADER ERROR - COMMAND (%s, %u bytes), peer=%d\n",
758-
hdr.GetCommand(), msg->m_message_size, m_node_id);
758+
SanitizeString(hdr.GetCommand()), msg->m_message_size, m_node_id);
759759
out_err_raw_size = msg->m_raw_message_size;
760760
msg.reset();
761761
}

test/functional/p2p_invalid_messages.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
class msg_unrecognized:
3838
"""Nonsensical message. Modeled after similar types in test_framework.messages."""
3939

40-
msgtype = b'badmsg'
40+
msgtype = b'badmsg\x01'
4141

4242
def __init__(self, *, str_data):
4343
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data
@@ -104,7 +104,7 @@ def test_duplicate_version_msg(self):
104104
def test_magic_bytes(self):
105105
self.log.info("Test message with invalid magic bytes disconnects peer")
106106
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
107-
with self.nodes[0].assert_debug_log(['HEADER ERROR - MESSAGESTART (badmsg, 2 bytes), received ffffffff']):
107+
with self.nodes[0].assert_debug_log(['HEADER ERROR - MESSAGESTART received ffffffff']):
108108
msg = conn.build_message(msg_unrecognized(str_data="d"))
109109
# modify magic bytes
110110
msg = b'\xff' * 4 + msg[4:]

0 commit comments

Comments
 (0)