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

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