Skip to content

Commit

Permalink
Fix the problem of memory growth caused by forged P2P message packets (
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull authored Jun 28, 2021
1 parent 8c1f492 commit 2ac180e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmake/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ macro(configure_project)
eth_default_option(ARCH_NATIVE OFF)

if(ARCH_NATIVE)
set(MARCH_TYPE "-march=native -mtune=native -fvisibility=hidden -fvisibility-inlines-hidden")
set(MARCH_TYPE "-march=native -mtune=generic -fvisibility=hidden -fvisibility-inlines-hidden")
endif()

# unit tests
Expand Down
13 changes: 7 additions & 6 deletions libchannelserver/ChannelMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ enum ChannelMessageType
CLIENT_HEARTBEAT = 0x13, // type for heart beat for sdk
CLIENT_HANDSHAKE = 0x14, // type for hand shake
CLIENT_REGISTER_EVENT_LOG = 0x15, // type for event log filter register request and response
CLIENT_UNREGISTER_EVENT_LOG = 0x16, // type for event log filter unregister request and response
CLIENT_UNREGISTER_EVENT_LOG = 0x16, // type for event log filter unregister request and
// response
AMOP_REQUEST = 0x30, // type for request from sdk
AMOP_RESPONSE = 0x31, // type for response to sdk
AMOP_CLIENT_SUBSCRIBE_TOPICS = 0x32, // type for topic request
Expand Down Expand Up @@ -93,13 +94,13 @@ class ChannelMessage : public Message
}

m_length = ntohl(*((uint32_t*)&buffer[0]));

#if 0
if (_length > MAX_LENGTH)
// invalid packet
if (m_length > MAX_LENGTH)
{
CHANNEL_LOG(WARNING) << LOG_DESC("Illegal message packet") << LOG_KV("length", m_length)
<< LOG_KV("maxLen", MAX_LENGTH);
return -1;
}
#endif

if (size < m_length)
{
Expand All @@ -119,7 +120,7 @@ class ChannelMessage : public Message
const static size_t MIN_HEADER_LENGTH = 4;

const static size_t HEADER_LENGTH = 4 + 2 + 32 + 4;
const static size_t MAX_LENGTH = ULONG_MAX; // max 4G
const static size_t MAX_LENGTH = 100 * 1024 * 1024; // max 100MB
};

class ChannelMessageFactory : public MessageFactory
Expand Down
10 changes: 5 additions & 5 deletions libp2p/P2PMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ ssize_t P2PMessage::decode(const byte* buffer, size_t size)

int32_t offset = 0;
m_length = ntohl(*((uint32_t*)&buffer[offset]));

/*if (m_length > MAX_LENGTH)
if (m_length > MAX_MESSAGE_LENGTH)
{
return PACKET_ERROR;
}*/

SESSION_LOG(WARNING) << LOG_DESC("Illegal p2p message packet") << LOG_KV("length", m_length)
<< LOG_KV("maxLen", MAX_MESSAGE_LENGTH);
return dev::network::PACKET_ERROR;
}
if (size < m_length)
{
return dev::network::PACKET_INCOMPLETE;
Expand Down
3 changes: 2 additions & 1 deletion libp2p/P2PMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class P2PMessage : public dev::network::Message
/// packetType: 2bytes
/// seq: 4 bytes
const static size_t HEADER_LENGTH = 12;
const static size_t MAX_LENGTH = 1024 * 1024; ///< The maximum length of data is 1M.
const static size_t MAX_MESSAGE_LENGTH =
100 * 1024 * 1024; ///< The maximum length of data is 100M.

P2PMessage() { m_buffer = std::make_shared<bytes>(); }

Expand Down
6 changes: 6 additions & 0 deletions libp2p/P2PMessageRC2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ ssize_t P2PMessageRC2::decode(const byte* buffer, size_t size)

int32_t offset = 0;
m_length = ntohl(*((uint32_t*)&buffer[offset]));
if (m_length > P2PMessage::MAX_MESSAGE_LENGTH)
{
SESSION_LOG(WARNING) << LOG_DESC("Illegal p2p message packet") << LOG_KV("length", m_length)
<< LOG_KV("maxLen", P2PMessage::MAX_MESSAGE_LENGTH);
return dev::network::PACKET_ERROR;
}

if (size < m_length)
{
Expand Down
1 change: 0 additions & 1 deletion libp2p/P2PMessageRC2.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class P2PMessageRC2 : public P2PMessage
/// m_length(4bytes) + m_version(2bytes) + m_protocolID(2bytes) + m_groupID(2bytes) +
/// m_packetType(2bytes) + m_seq(4bytes)
const static size_t HEADER_LENGTH = 16;
const static size_t MAX_LENGTH = 1024 * 1024; ///< The maximum length of data is 1M.

P2PMessageRC2()
{
Expand Down
2 changes: 1 addition & 1 deletion libsync/SyncMsgEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using namespace dev::p2p;
using namespace dev::blockchain;
using namespace dev::txpool;

static size_t const c_maxPayload = dev::p2p::P2PMessage::MAX_LENGTH - 2048;
static size_t const c_maxPayload = dev::p2p::P2PMessage::MAX_MESSAGE_LENGTH - 2048;

void SyncMsgEngine::messageHandler(
NetworkException, std::shared_ptr<dev::p2p::P2PSession> _session, P2PMessage::Ptr _msg)
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/libsync/SyncMsgEngineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ BOOST_AUTO_TEST_CASE(SyncReqBlockPacketTest)
BOOST_AUTO_TEST_CASE(BatchSendTest)
{
size_t maxPayloadSize =
dev::p2p::P2PMessage::MAX_LENGTH - 2048; // should be the same as syncMsgEngine.cpp
dev::p2p::P2PMessage::MAX_MESSAGE_LENGTH - 2048; // should be the same as syncMsgEngine.cpp
size_t quarterPayloadSize = maxPayloadSize / 4;

FakeServiceForDownloadBlocksContainer::Ptr service =
Expand Down

0 comments on commit 2ac180e

Please sign in to comment.