54d6deb
From 5d201df72f3d4f4cb8b8f75f980169b03507da38 Mon Sep 17 00:00:00 2001
54d6deb
From: Tobias Stoeckmann <tobias@stoeckmann.org>
54d6deb
Date: Tue, 28 Nov 2017 21:38:07 +0100
54d6deb
Subject: [PATCH] cursor: Fix heap overflows when parsing malicious files.
54d6deb
54d6deb
It is possible to trigger heap overflows due to an integer overflow
54d6deb
while parsing images.
54d6deb
54d6deb
The integer overflow occurs because the chosen limit 0x10000 for
54d6deb
dimensions is too large for 32 bit systems, because each pixel takes
54d6deb
4 bytes. Properly chosen values allow an overflow which in turn will
54d6deb
lead to less allocated memory than needed for subsequent reads.
54d6deb
54d6deb
See also: https://cgit.freedesktop.org/xorg/lib/libXcursor/commit/?id=4794b5dd34688158fb51a2943032569d3780c4b8
54d6deb
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=103961
54d6deb
54d6deb
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
54d6deb
[Pekka: add link to the corresponding libXcursor commit]
54d6deb
Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
54d6deb
---
54d6deb
 cursor/xcursor.c | 8 +++++++-
54d6deb
 1 file changed, 7 insertions(+), 1 deletion(-)
54d6deb
54d6deb
diff --git a/cursor/xcursor.c b/cursor/xcursor.c
54d6deb
index ca41c4ac611f..689c7026729d 100644
54d6deb
--- a/cursor/xcursor.c
54d6deb
+++ b/cursor/xcursor.c
54d6deb
@@ -202,6 +202,11 @@ XcursorImageCreate (int width, int height)
54d6deb
 {
54d6deb
     XcursorImage    *image;
54d6deb
 
54d6deb
+    if (width < 0 || height < 0)
54d6deb
+       return NULL;
54d6deb
+    if (width > XCURSOR_IMAGE_MAX_SIZE || height > XCURSOR_IMAGE_MAX_SIZE)
54d6deb
+       return NULL;
54d6deb
+
54d6deb
     image = malloc (sizeof (XcursorImage) +
54d6deb
 		    width * height * sizeof (XcursorPixel));
54d6deb
     if (!image)
54d6deb
@@ -482,7 +487,8 @@ _XcursorReadImage (XcursorFile		*file,
54d6deb
     if (!_XcursorReadUInt (file, &head.delay))
54d6deb
 	return NULL;
54d6deb
     /* sanity check data */
54d6deb
-    if (head.width >= 0x10000 || head.height > 0x10000)
54d6deb
+    if (head.width > XCURSOR_IMAGE_MAX_SIZE  ||
54d6deb
+	head.height > XCURSOR_IMAGE_MAX_SIZE)
54d6deb
 	return NULL;
54d6deb
     if (head.width == 0 || head.height == 0)
54d6deb
 	return NULL;
54d6deb
-- 
54d6deb
2.14.3
54d6deb