be0570
From 5e32c045e91ed9b0609154f7be2c3366dde1a282 Mon Sep 17 00:00:00 2001
be0570
From: Nils Philippsen <nils@redhat.com>
be0570
Date: Fri, 29 Nov 2013 10:40:08 +0100
be0570
Subject: [PATCH] patch: CVE-2013-1913,1978
be0570
be0570
Squashed commit of the following:
be0570
be0570
commit 1f1e33f606e50f9efa338c92d036d837182dbfd5
be0570
Author: Nils Philippsen <nils@redhat.com>
be0570
Date:   Tue Nov 26 10:49:42 2013 +0100
be0570
be0570
    file-xwd: sanity check # of colors and map entries (CVE-2013-1978)
be0570
be0570
    The number of colors in an image shouldn't be higher than the number of
be0570
    colormap entries. Additionally, consolidate post error cleanup in
be0570
    load_image().
be0570
be0570
    (cherry picked from commit f597355beffd9e483e11407d4c3b56f32db3634d)
be0570
be0570
commit 005e17a83907d89b37a432b3edf458a7c82f78bf
be0570
Author: Nils Philippsen <nils@redhat.com>
be0570
Date:   Thu Nov 14 14:29:01 2013 +0100
be0570
be0570
    file-xwd: sanity check colormap size (CVE-2013-1913)
be0570
be0570
    (cherry picked from commit 3997c7188a71dc8fc4c6a7513061180cbbd3590e)
be0570
---
be0570
 plug-ins/common/file-xwd.c | 62 +++++++++++++++++++++++++++-------------------
be0570
 1 file changed, 37 insertions(+), 25 deletions(-)
be0570
be0570
diff --git a/plug-ins/common/file-xwd.c b/plug-ins/common/file-xwd.c
be0570
index 3240f7e..ba07afd 100644
be0570
--- a/plug-ins/common/file-xwd.c
be0570
+++ b/plug-ins/common/file-xwd.c
be0570
@@ -424,9 +424,9 @@ static gint32
be0570
 load_image (const gchar  *filename,
be0570
             GError      **error)
be0570
 {
be0570
-  FILE            *ifp;
be0570
+  FILE            *ifp = NULL;
be0570
   gint             depth, bpp;
be0570
-  gint32           image_ID;
be0570
+  gint32           image_ID = -1;
be0570
   L_XWDFILEHEADER  xwdhdr;
be0570
   L_XWDCOLOR      *xwdcolmap = NULL;
be0570
 
be0570
@@ -436,7 +436,7 @@ load_image (const gchar  *filename,
be0570
       g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
be0570
                    _("Could not open '%s' for reading: %s"),
be0570
                    gimp_filename_to_utf8 (filename), g_strerror (errno));
be0570
-      return -1;
be0570
+      goto out;
be0570
     }
be0570
 
be0570
   read_xwd_header (ifp, &xwdhdr);
be0570
@@ -445,8 +445,7 @@ load_image (const gchar  *filename,
be0570
       g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
be0570
                    _("Could not read XWD header from '%s'"),
be0570
                    gimp_filename_to_utf8 (filename));
be0570
-      fclose (ifp);
be0570
-      return -1;
be0570
+      goto out;
be0570
     }
be0570
 
be0570
 #ifdef XWD_COL_WAIT_DEBUG
be0570
@@ -461,8 +460,25 @@ load_image (const gchar  *filename,
be0570
   /* Position to start of XWDColor structures */
be0570
   fseek (ifp, (long)xwdhdr.l_header_size, SEEK_SET);
be0570
 
be0570
+  /* Guard against insanely huge color maps -- gimp_image_set_colormap() only
be0570
+   * accepts colormaps with 0..256 colors anyway. */
be0570
+  if (xwdhdr.l_colormap_entries > 256)
be0570
+    {
be0570
+      g_message (_("'%s':\nIllegal number of colormap entries: %ld"),
be0570
+                 gimp_filename_to_utf8 (filename),
be0570
+                 (long)xwdhdr.l_colormap_entries);
be0570
+      goto out;
be0570
+    }
be0570
+
be0570
   if (xwdhdr.l_colormap_entries > 0)
be0570
     {
be0570
+      if (xwdhdr.l_colormap_entries < xwdhdr.l_ncolors)
be0570
+        {
be0570
+          g_message (_("'%s':\nNumber of colormap entries < number of colors"),
be0570
+                     gimp_filename_to_utf8 (filename));
be0570
+          goto out;
be0570
+        }
be0570
+
be0570
       xwdcolmap = g_new (L_XWDCOLOR, xwdhdr.l_colormap_entries);
be0570
 
be0570
       read_xwd_cols (ifp, &xwdhdr, xwdcolmap);
be0570
@@ -482,9 +498,7 @@ load_image (const gchar  *filename,
be0570
       if (xwdhdr.l_file_version != 7)
be0570
         {
be0570
           g_message (_("Can't read color entries"));
be0570
-          g_free (xwdcolmap);
be0570
-          fclose (ifp);
be0570
-          return (-1);
be0570
+          goto out;
be0570
         }
be0570
     }
be0570
 
be0570
@@ -492,9 +506,7 @@ load_image (const gchar  *filename,
be0570
     {
be0570
       g_message (_("'%s':\nNo image width specified"),
be0570
                  gimp_filename_to_utf8 (filename));
be0570
-      g_free (xwdcolmap);
be0570
-      fclose (ifp);
be0570
-      return (-1);
be0570
+      goto out;
be0570
     }
be0570
 
be0570
   if (xwdhdr.l_pixmap_width > GIMP_MAX_IMAGE_SIZE
be0570
@@ -502,27 +514,21 @@ load_image (const gchar  *filename,
be0570
     {
be0570
       g_message (_("'%s':\nImage width is larger than GIMP can handle"),
be0570
                  gimp_filename_to_utf8 (filename));
be0570
-      g_free (xwdcolmap);
be0570
-      fclose (ifp);
be0570
-      return (-1);
be0570
+      goto out;
be0570
     }
be0570
 
be0570
   if (xwdhdr.l_pixmap_height <= 0)
be0570
     {
be0570
       g_message (_("'%s':\nNo image height specified"),
be0570
                  gimp_filename_to_utf8 (filename));
be0570
-      g_free (xwdcolmap);
be0570
-      fclose (ifp);
be0570
-      return (-1);
be0570
+      goto out;
be0570
     }
be0570
 
be0570
   if (xwdhdr.l_pixmap_height > GIMP_MAX_IMAGE_SIZE)
be0570
     {
be0570
       g_message (_("'%s':\nImage height is larger than GIMP can handle"),
be0570
                  gimp_filename_to_utf8 (filename));
be0570
-      g_free (xwdcolmap);
be0570
-      fclose (ifp);
be0570
-      return (-1);
be0570
+      goto out;
be0570
     }
be0570
 
be0570
   gimp_progress_init_printf (_("Opening '%s'"),
be0570
@@ -571,11 +577,6 @@ load_image (const gchar  *filename,
be0570
     }
be0570
   gimp_progress_update (1.0);
be0570
 
be0570
-  fclose (ifp);
be0570
-
be0570
-  if (xwdcolmap)
be0570
-    g_free (xwdcolmap);
be0570
-
be0570
   if (image_ID == -1 && ! (error && *error))
be0570
     g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
be0570
                  _("XWD-file %s has format %d, depth %d and bits per pixel %d. "
be0570
@@ -583,6 +584,17 @@ load_image (const gchar  *filename,
be0570
                  gimp_filename_to_utf8 (filename),
be0570
                  (gint) xwdhdr.l_pixmap_format, depth, bpp);
be0570
 
be0570
+out:
be0570
+  if (ifp)
be0570
+    {
be0570
+      fclose (ifp);
be0570
+    }
be0570
+
be0570
+  if (xwdcolmap)
be0570
+    {
be0570
+      g_free (xwdcolmap);
be0570
+    }
be0570
+
be0570
   return image_ID;
be0570
 }
be0570
 
be0570
-- 
be0570
1.8.4.2
be0570