🚨 Validate decompressed packets before dispatch

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.
This commit is contained in:
2026-11-15 22:08:32 +01:00
parent d18e01c95c
commit c4c95b0156
+11 -2
View File
@@ -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;