7c7644a
From beef32b0e510371f3c968d22a1e3d48abbf366c6 Mon Sep 17 00:00:00 2001
7c7644a
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
7c7644a
Date: Tue, 19 Feb 2019 14:52:52 +0100
7c7644a
Subject: [PATCH] CVE-2019-7635: Reject BMP images with pixel colors out the
7c7644a
 palette
7c7644a
MIME-Version: 1.0
7c7644a
Content-Type: text/plain; charset=UTF-8
7c7644a
Content-Transfer-Encoding: 8bit
7c7644a
7c7644a
If a 1-, 4-, or 8-bit per pixel BMP image declares less used colors
7c7644a
than the palette offers an SDL_Surface with a palette of the indicated
7c7644a
number of used colors is created. If some of the image's pixel
7c7644a
refer to a color number higher then the maximal used colors, a subsequent
7c7644a
bliting operation on the surface will look up a color past a blit map
7c7644a
(that is based on the palette) memory. I.e. passing such SDL_Surface
7c7644a
to e.g. an SDL_DisplayFormat() function will result in a buffer overread in
7c7644a
a blit function.
7c7644a
7c7644a
This patch fixes it by validing each pixel's color to be less than the
7c7644a
maximal color number in the palette. A validation failure raises an
7c7644a
error from a SDL_LoadBMP_RW() function.
7c7644a
7c7644a
CVE-2019-7635
7c7644a
https://bugzilla.libsdl.org/show_bug.cgi?id=4498
7c7644a
7c7644a
Signed-off-by: Petr Písař <ppisar@redhat.com>
7c7644a
---
7c7644a
 src/video/SDL_bmp.c | 16 ++++++++++++++++
7c7644a
 1 file changed, 16 insertions(+)
7c7644a
7c7644a
diff --git a/src/video/SDL_bmp.c b/src/video/SDL_bmp.c
7c7644a
index 3accded..8eadc5f 100644
7c7644a
--- a/src/video/SDL_bmp.c
7c7644a
+++ b/src/video/SDL_bmp.c
7c7644a
@@ -300,6 +300,12 @@ SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
7c7644a
 				}
7c7644a
 				*(bits+i) = (pixel>>shift);
7c7644a
 				pixel <<= ExpandBMP;
7c7644a
+				if ( bits[i] >= biClrUsed ) {
7c7644a
+					SDL_SetError(
7c7644a
+						"A BMP image contains a pixel with a color out of the palette");
7c7644a
+					was_error = SDL_TRUE;
7c7644a
+					goto done;
7c7644a
+				}
7c7644a
 			} }
7c7644a
 			break;
7c7644a
 
7c7644a
@@ -310,6 +316,16 @@ SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
7c7644a
 				was_error = SDL_TRUE;
7c7644a
 				goto done;
7c7644a
 			}
7c7644a
+			if ( 8 == biBitCount && palette && biClrUsed < (1 << biBitCount ) ) {
7c7644a
+				for ( i=0; i<surface->w; ++i ) {
7c7644a
+					if ( bits[i] >= biClrUsed ) {
7c7644a
+						SDL_SetError(
7c7644a
+							"A BMP image contains a pixel with a color out of the palette");
7c7644a
+						was_error = SDL_TRUE;
7c7644a
+						goto done;
7c7644a
+					}
7c7644a
+				}
7c7644a
+			}
7c7644a
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
7c7644a
 			/* Byte-swap the pixels if needed. Note that the 24bpp
7c7644a
 			   case has already been taken care of above. */
7c7644a
-- 
7c7644a
2.20.1
7c7644a