From 8593be1b6a114e7cd64ad0db800d88fa2f4a6c9a Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 4 Jun 2015 14:53:27 +0200 Subject: [PATCH 1/3] Log warning and disconnect when remote passes bad rlp during handshake. --- libp2p/RLPxHandshake.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libp2p/RLPxHandshake.cpp b/libp2p/RLPxHandshake.cpp index 65116cd58..d7c2e5e3b 100644 --- a/libp2p/RLPxHandshake.cpp +++ b/libp2p/RLPxHandshake.cpp @@ -265,8 +265,17 @@ void RLPXHandshake::transition(boost::system::error_code _ech) } clog(NetTriviaSummary) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "hello frame: success. starting session."; - RLP rlp(frame.cropped(1), RLP::ThrowOnFail | RLP::FailIfTooSmall); - m_host->startPeerSession(m_remote, rlp, m_io, m_socket->remoteEndpoint()); + try + { + RLP rlp(frame.cropped(1), RLP::ThrowOnFail | RLP::FailIfTooSmall); + m_host->startPeerSession(m_remote, rlp, m_io, m_socket->remoteEndpoint()); + } + catch (std::exception const& _e) + { + clog(NetWarn) << "Handshake causing an exception:" << _e.what(); + m_nextState = Error; + transition(); + } } }); } From f26bbb21e491e0d1492570d87027c1edb05307e9 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 4 Jun 2015 16:10:25 +0200 Subject: [PATCH 2/3] Drop socket and log when peer disconnect occurs mid-read. --- libp2p/Session.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index 2a16007ca..462fea7b1 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -447,8 +447,12 @@ void Session::doRead() clog(NetWarn) << "Error reading: " << ec.message(); drop(TCPError); } - else if (ec && length == 0) + else if (ec && length < tlen) + { + clog(NetWarn) << "Error reading - Abrupt peer disconnect: " << ec.message(); + drop(TCPError); return; + } else { if (!m_io->authAndDecryptFrame(bytesRef(m_data.data(), tlen))) From 3a5e67eaabf531a8b2ea36369a25e98d6a20da6b Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 4 Jun 2015 17:59:41 +0200 Subject: [PATCH 3/3] Fix network tests: delete host pointers after use. --- test/libp2p/peer.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/libp2p/peer.cpp b/test/libp2p/peer.cpp index 192dacd7b..a5a4a64dc 100644 --- a/test/libp2p/peer.cpp +++ b/test/libp2p/peer.cpp @@ -116,6 +116,9 @@ BOOST_AUTO_TEST_CASE(saveNodes) BOOST_REQUIRE(i.itemCount() == 4 || i.itemCount() == 11); BOOST_REQUIRE(i[0].size() == 4 || i[0].size() == 16); } + + for (auto host: hosts) + delete host; } BOOST_AUTO_TEST_SUITE_END()