🔒 Validate compressed packets on the server

A client could declare an unbounded decompressed size to exhaust
server memory, nest compressed packets recursively, or send a
truncated inner packet whose handler read garbage.
This commit is contained in:
2026-11-15 22:08:32 +01:00
parent 7acd461086
commit e12fe942b7
+17 -2
View File
@@ -133,19 +133,34 @@ var
uncompStream: TEnhancedMemoryStream;
uncompBuffer: TDecompressionStream;
targetSize: Cardinal;
innerSize: Cardinal;
packetID: Byte;
begin
targetSize := ABuffer.ReadCardinal;
if (targetSize < 5) or (targetSize > 64 * 1024 * 1024) then
begin
Writeln(TimeStamp, 'Dropping client due to invalid compressed packet size: ', ANetState.Socket.PeerAddress);
ANetState.ReceiveQueue.Clear;
CEDServerInstance.Disconnect(ANetState.Socket);
Exit;
end;
uncompBuffer := TDecompressionStream.Create(ABuffer);
uncompStream := TEnhancedMemoryStream.Create;
try
uncompStream.CopyFrom(uncompBuffer, targetSize);
uncompStream.Position := 0;
packetID := uncompStream.ReadByte;
if PacketHandlers[packetID] <> nil then
if (packetID <> $01) and (PacketHandlers[packetID] <> nil) then
begin
if PacketHandlers[PacketID].PacketLength = 0 then
uncompStream.Position := uncompStream.Position + 4;
begin
innerSize := uncompStream.ReadCardinal;
if (innerSize < 5) or
(uncompStream.Size - uncompStream.Position < innerSize - 5) then
Exit;
end else if uncompStream.Size - uncompStream.Position <
PacketHandlers[PacketID].PacketLength - 1 then
Exit;
uncompStream.Lock(uncompStream.Position, uncompStream.Size - uncompStream.Position);
PacketHandlers[PacketID].Process(uncompStream, ANetState);
uncompStream.Unlock;