Blame SOURCES/Added-missing-length-checks-in-zgfx_decompress_segme.patch

087af1
From babbd1e433d273634637f5199429986714864033 Mon Sep 17 00:00:00 2001
087af1
From: akallabeth <akallabeth@posteo.net>
087af1
Date: Thu, 13 Oct 2022 09:09:28 +0200
087af1
Subject: [PATCH] Added missing length checks in zgfx_decompress_segment
087af1
087af1
(cherry picked from commit 64716b335858109d14f27b51acc4c4d71a92a816)
087af1
---
087af1
 libfreerdp/codec/zgfx.c | 11 +++++++----
087af1
 1 file changed, 7 insertions(+), 4 deletions(-)
087af1
087af1
diff --git a/libfreerdp/codec/zgfx.c b/libfreerdp/codec/zgfx.c
087af1
index 1a2878bd9..04ddeadb2 100644
087af1
--- a/libfreerdp/codec/zgfx.c
087af1
+++ b/libfreerdp/codec/zgfx.c
087af1
@@ -230,19 +230,19 @@ static BOOL zgfx_decompress_segment(ZGFX_CONTEXT* zgfx, wStream* stream, size_t
087af1
 	BYTE* pbSegment;
087af1
 	size_t cbSegment;
087af1
 
087af1
-	if (!zgfx || !stream)
087af1
+	if (!zgfx || !stream || (segmentSize < 2))
087af1
 		return FALSE;
087af1
 
087af1
 	cbSegment = segmentSize - 1;
087af1
 
087af1
-	if ((Stream_GetRemainingLength(stream) < segmentSize) || (segmentSize < 1) ||
087af1
-	    (segmentSize > UINT32_MAX))
087af1
+	if ((Stream_GetRemainingLength(stream) < segmentSize) || (segmentSize > UINT32_MAX))
087af1
 		return FALSE;
087af1
 
087af1
 	Stream_Read_UINT8(stream, flags); /* header (1 byte) */
087af1
 	zgfx->OutputCount = 0;
087af1
 	pbSegment = Stream_Pointer(stream);
087af1
-	Stream_Seek(stream, cbSegment);
087af1
+	if (!Stream_SafeSeek(stream, cbSegment))
087af1
+		return FALSE;
087af1
 
087af1
 	if (!(flags & PACKET_COMPRESSED))
087af1
 	{
087af1
@@ -346,6 +346,9 @@ static BOOL zgfx_decompress_segment(ZGFX_CONTEXT* zgfx, wStream* stream, size_t
087af1
 						if (count > sizeof(zgfx->OutputBuffer) - zgfx->OutputCount)
087af1
 							return FALSE;
087af1
 
087af1
+						if (count > zgfx->cBitsRemaining / 8)
087af1
+							return FALSE;
087af1
+
087af1
 						CopyMemory(&(zgfx->OutputBuffer[zgfx->OutputCount]), zgfx->pbInputCurrent,
087af1
 						           count);
087af1
 						zgfx_history_buffer_ring_write(zgfx, zgfx->pbInputCurrent, count);
087af1
-- 
087af1
2.37.1
087af1