Skip to content

Commit

Permalink
sentry: treat invalid snappy data (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored Jan 23, 2024
1 parent 7280671 commit 4780733
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion silkworm/sentry/common/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#pragma once

#include <silkworm/core/common/base.hpp>
#include <cstdint>

#include <silkworm/core/common/bytes.hpp>

namespace silkworm::sentry {
Expand Down
1 change: 1 addition & 0 deletions silkworm/sentry/rlpx/common/disconnect_reason.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace silkworm::sentry::rlpx {
enum class DisconnectReason : uint8_t {
DisconnectRequested = 0,
NetworkError = 1,
ProtocolError = 2,
UselessPeer = 3,
TooManyPeers = 4,
ClientQuitting = 8,
Expand Down
2 changes: 1 addition & 1 deletion silkworm/sentry/rlpx/framing/message_frame_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static Bytes snappy_decompress(ByteView data) {
data.size(),
reinterpret_cast<char*>(output.data()));
if (!ok)
throw std::runtime_error("MessageFrameCodec: invalid snappy data");
throw MessageFrameCodec::DecompressionError();
return output;
}

Expand Down
8 changes: 8 additions & 0 deletions silkworm/sentry/rlpx/framing/message_frame_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#pragma once

#include <stdexcept>

#include <silkworm/core/common/bytes.hpp>
#include <silkworm/sentry/common/message.hpp>

namespace silkworm::sentry::rlpx::framing {
Expand All @@ -27,6 +30,11 @@ class MessageFrameCodec {

void enable_compression() { is_compression_enabled_ = true; }

class DecompressionError : public std::runtime_error {
public:
DecompressionError() : std::runtime_error("MessageFrameCodec: invalid snappy data") {}
};

static const size_t kMaxFrameSize;

private:
Expand Down
2 changes: 2 additions & 0 deletions silkworm/sentry/rlpx/framing/message_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class MessageStream {

void enable_compression();

using DecompressionError = MessageFrameCodec::DecompressionError;

private:
FramingCipher cipher_;
SocketStream& stream_;
Expand Down
3 changes: 3 additions & 0 deletions silkworm/sentry/rlpx/peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ Task<void> Peer::handle() {
} catch (const auth::Handshake::DisconnectError& ex) {
log::Debug("sentry") << "Peer::handle DisconnectError reason: " << static_cast<int>(ex.reason());
disconnect_reason_.set({ex.reason()});
} catch (const framing::MessageStream::DecompressionError& ex) {
log::Debug("sentry") << "Peer::handle DecompressionError: " << ex.what();
disconnect_reason_.set({DisconnectReason::ProtocolError});
} catch (const auth::Handshake::CapabilityMismatchError& ex) {
log::Debug("sentry") << "Peer::handle CapabilityMismatchError: " << ex.what();
disconnect_reason_.set({DisconnectReason::UselessPeer});
Expand Down

0 comments on commit 4780733

Please sign in to comment.