From c4c95b0156c4cf45e6f22be417ac8ec3486d3fc9 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 15 Nov 2026 09:24:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20Validate=20decompressed=20packet?= =?UTF-8?q?s=20before=20dispatch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A compressed packet could declare an unbounded decompressed size, nest compressed packets recursively until stack overflow, or carry a truncated inner packet whose handler then read uninitialized memory. --- Client/UPacketHandlers.pas | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Client/UPacketHandlers.pas b/Client/UPacketHandlers.pas index d42077a..8b1cd40 100644 --- a/Client/UPacketHandlers.pas +++ b/Client/UPacketHandlers.pas @@ -96,21 +96,30 @@ var uncompStream: TEnhancedMemoryStream; uncompBuffer: TDecompressionStream; targetSize: Cardinal; + innerSize: Cardinal; packetID: Byte; begin //writeln('compressed size: ', ABuffer.Size); targetSize := ABuffer.ReadCardinal; //writeln('uncompressed size: ', targetSize); + if (targetSize < 5) or (targetSize > 64 * 1024 * 1024) then Exit; 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); uncompStream.Unlock;