Network: refuse packet sizes no real client sends, before login too
bnetserver: a header could ask for up to 4 GB and the session allocated it (bad_alloc ends the process); sizes over 1 MB now close the connection. worldserver: a packet header with a size below 4 wrote the opcode past the end of a zero-sized buffer; it now closes the connection.
This commit is contained in:
@@ -70,6 +70,7 @@ void Battlenet::GameAccountInfo::LoadResult(Field const* fields)
|
||||
}
|
||||
|
||||
constexpr std::size_t PacketHeaderLengthSize = sizeof(uint16);
|
||||
constexpr std::size_t MaxPacketDataSize = 0x100000; // client messages are small; only a broken or hostile client asks for more
|
||||
|
||||
Battlenet::Session::Session(Trinity::Net::IoContextTcpSocket&& socket) : _socket(CreateSocket(std::move(socket))),
|
||||
_packetReadState(PacketReadState::HeaderLength), _packetBuffer(PacketHeaderLengthSize),
|
||||
@@ -315,6 +316,13 @@ bool Battlenet::Session::ReadHeaderHandler()
|
||||
|
||||
_packetBuffer.ReadCompleted(_packetBuffer.GetActiveSize());
|
||||
|
||||
// the size is the client's own number: refuse anything a real message never needs instead of allocating it
|
||||
if (header.size() > MaxPacketDataSize)
|
||||
{
|
||||
TC_LOG_ERROR("session", "{} sent a packet header with size {}, closing the connection", GetClientInfo(), header.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
_packetReadState = PacketReadState::Data;
|
||||
_packetBuffer.Resize(_packetBuffer.GetBufferSize() + header.size());
|
||||
return true;
|
||||
|
||||
@@ -343,6 +343,13 @@ bool WorldSocket::ReadHeaderHandler()
|
||||
IncomingPacketHeader* header = reinterpret_cast<IncomingPacketHeader*>(_headerBuffer.GetReadPointer());
|
||||
uint32 encryptedOpcode = header->EncryptedOpcode;
|
||||
|
||||
// the size covers the opcode that is written into the buffer below
|
||||
if (header->Size < sizeof(encryptedOpcode))
|
||||
{
|
||||
TC_LOG_ERROR("network", "WorldSocket::ReadHeaderHandler(): client {} sent malformed packet (size: {})", GetRemoteIpAddress(), header->Size);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!header->IsValidSize())
|
||||
{
|
||||
_authCrypt.PeekDecryptRecv(reinterpret_cast<uint8*>(&header->EncryptedOpcode), sizeof(encryptedOpcode));
|
||||
|
||||
Reference in New Issue
Block a user