Blame SOURCES/Add-checks-for-bitmap-and-glyph-width-heigth-values.patch

35d644
From 4a4ecc32b160e93ee1e37e5617a0197e2d022643 Mon Sep 17 00:00:00 2001
35d644
From: Armin Novak <armin.novak@thincast.com>
35d644
Date: Thu, 4 Nov 2021 11:07:41 +0100
35d644
Subject: [PATCH] Add checks for bitmap and glyph width/heigth values
35d644
35d644
CVE-2021-41160
35d644
35d644
https://github.com/FreeRDP/FreeRDP/pull/7349
35d644
35d644
Signed-off-by: Felipe Borges <felipeborges@gnome.org>
35d644
---
35d644
 libfreerdp/core/orders.c  | 14 ++++++++++++
35d644
 libfreerdp/core/surface.c | 45 +++++++++++++++++++++++++++++++++++++++
35d644
 libfreerdp/core/update.c  |  7 ++++++
35d644
 3 files changed, 66 insertions(+)
35d644
35d644
diff --git a/libfreerdp/core/orders.c b/libfreerdp/core/orders.c
35d644
index 74870fae6..44d23a61a 100644
35d644
--- a/libfreerdp/core/orders.c
35d644
+++ b/libfreerdp/core/orders.c
35d644
@@ -1848,6 +1848,13 @@ static BOOL update_read_fast_glyph_order(wStream* s, const ORDER_INFO* orderInfo
35d644
 			new_cb = ((glyph->cx + 7) / 8) * glyph->cy;
35d644
 			new_cb += ((new_cb % 4) > 0) ? 4 - (new_cb % 4) : 0;
35d644
 
35d644
+			if ((glyph->cx == 0) || (glyph->cy == 0))
35d644
+			{
35d644
+				WLog_ERR(TAG, "GLYPH_DATA_V2::cx=%" PRIu32 ", GLYPH_DATA_V2::cy=%" PRIu32,
35d644
+				         glyph->cx, glyph->cy);
35d644
+				return FALSE;
35d644
+			}
35d644
+
35d644
 			if (fastGlyph->cbData < new_cb)
35d644
 				return FALSE;
35d644
 
35d644
@@ -2825,6 +2832,13 @@ update_read_create_offscreen_bitmap_order(wStream* s,
35d644
 	Stream_Read_UINT16(s, create_offscreen_bitmap->cy); /* cy (2 bytes) */
35d644
 	deleteList = &(create_offscreen_bitmap->deleteList);
35d644
 
35d644
+	if ((create_offscreen_bitmap->cx == 0) || (create_offscreen_bitmap->cy == 0))
35d644
+	{
35d644
+		WLog_ERR(TAG, "Invalid OFFSCREEN_DELETE_LIST: cx=%" PRIu16 ", cy=%" PRIu16,
35d644
+		         create_offscreen_bitmap->cx, create_offscreen_bitmap->cy);
35d644
+		return FALSE;
35d644
+	}
35d644
+
35d644
 	if (deleteListPresent)
35d644
 	{
35d644
 		UINT32 i;
35d644
diff --git a/libfreerdp/core/surface.c b/libfreerdp/core/surface.c
35d644
index d5c709885..ca89d230c 100644
35d644
--- a/libfreerdp/core/surface.c
35d644
+++ b/libfreerdp/core/surface.c
35d644
@@ -21,6 +21,8 @@
35d644
 #include "config.h"
35d644
 #endif
35d644
 
35d644
+#include <assert.h>
35d644
+
35d644
 #include <freerdp/utils/pcap.h>
35d644
 #include <freerdp/log.h>
35d644
 
35d644
@@ -62,6 +64,13 @@ static BOOL update_recv_surfcmd_bitmap_ex(wStream* s, TS_BITMAP_DATA_EX* bmp)
35d644
 	Stream_Read_UINT16(s, bmp->height);
35d644
 	Stream_Read_UINT32(s, bmp->bitmapDataLength);
35d644
 
35d644
+	if ((bmp->width == 0) || (bmp->height == 0))
35d644
+	{
35d644
+		WLog_ERR(TAG, "invalid size value width=%" PRIu16 ", height=%" PRIu16, bmp->width,
35d644
+		         bmp->height);
35d644
+		return FALSE;
35d644
+	}
35d644
+
35d644
 	if ((bmp->bpp < 1) || (bmp->bpp > 32))
35d644
 	{
35d644
 		WLog_ERR(TAG, "invalid bpp value %" PRIu32 "", bmp->bpp);
35d644
@@ -85,6 +94,39 @@ static BOOL update_recv_surfcmd_bitmap_ex(wStream* s, TS_BITMAP_DATA_EX* bmp)
35d644
 	return TRUE;
35d644
 }
35d644
 
35d644
+static BOOL update_recv_surfcmd_is_rect_valid(const rdpContext* context,
35d644
+                                              const SURFACE_BITS_COMMAND* cmd)
35d644
+{
35d644
+	assert(context);
35d644
+	assert(context->settings);
35d644
+	assert(cmd);
35d644
+
35d644
+	/* We need a rectangle with left/top being smaller than right/bottom.
35d644
+	 * Also do not allow empty rectangles. */
35d644
+	if ((cmd->destTop >= cmd->destBottom) || (cmd->destLeft >= cmd->destRight))
35d644
+	{
35d644
+		WLog_WARN(TAG,
35d644
+		          "Empty surface bits command rectangle: %" PRIu16 "x%" PRIu16 "-%" PRIu16
35d644
+		          "x%" PRIu16,
35d644
+		          cmd->destLeft, cmd->destTop, cmd->destRight, cmd->destBottom);
35d644
+		return FALSE;
35d644
+	}
35d644
+
35d644
+	/* The rectangle needs to fit into our session size */
35d644
+	if ((cmd->destRight > context->settings->DesktopWidth) ||
35d644
+	    (cmd->destBottom > context->settings->DesktopHeight))
35d644
+	{
35d644
+		WLog_WARN(TAG,
35d644
+		          "Invalid surface bits command rectangle: %" PRIu16 "x%" PRIu16 "-%" PRIu16
35d644
+		          "x%" PRIu16 " does not fit %" PRIu32 "x%" PRIu32,
35d644
+		          cmd->destLeft, cmd->destTop, cmd->destRight, cmd->destBottom,
35d644
+		          context->settings->DesktopWidth, context->settings->DesktopHeight);
35d644
+		return FALSE;
35d644
+	}
35d644
+
35d644
+	return TRUE;
35d644
+}
35d644
+
35d644
 static BOOL update_recv_surfcmd_surface_bits(rdpUpdate* update, wStream* s, UINT16 cmdType)
35d644
 {
35d644
 	SURFACE_BITS_COMMAND cmd = { 0 };
35d644
@@ -98,6 +140,9 @@ static BOOL update_recv_surfcmd_surface_bits(rdpUpdate* update, wStream* s, UINT
35d644
 	Stream_Read_UINT16(s, cmd.destRight);
35d644
 	Stream_Read_UINT16(s, cmd.destBottom);
35d644
 
35d644
+	if (!update_recv_surfcmd_is_rect_valid(update->context, &cmd))
35d644
+		goto fail;
35d644
+
35d644
 	if (!update_recv_surfcmd_bitmap_ex(s, &cmd.bmp))
35d644
 		goto fail;
35d644
 
35d644
diff --git a/libfreerdp/core/update.c b/libfreerdp/core/update.c
35d644
index ebb82fc2d..e137c3de8 100644
35d644
--- a/libfreerdp/core/update.c
35d644
+++ b/libfreerdp/core/update.c
35d644
@@ -99,6 +99,13 @@ static BOOL update_read_bitmap_data(rdpUpdate* update, wStream* s, BITMAP_DATA*
35d644
 	Stream_Read_UINT16(s, bitmapData->flags);
35d644
 	Stream_Read_UINT16(s, bitmapData->bitmapLength);
35d644
 
35d644
+	if ((bitmapData->width == 0) || (bitmapData->height == 0))
35d644
+	{
35d644
+		WLog_ERR(TAG, "Invalid BITMAP_DATA: width=%" PRIu16 ", height=%" PRIu16, bitmapData->width,
35d644
+		         bitmapData->height);
35d644
+		return FALSE;
35d644
+	}
35d644
+
35d644
 	if (bitmapData->flags & BITMAP_COMPRESSION)
35d644
 	{
35d644
 		if (!(bitmapData->flags & NO_BITMAP_COMPRESSION_HDR))
35d644
-- 
35d644
2.32.0
35d644