Blame SOURCES/gegl-0.2.0-CVE-2012-4433.patch

0b49ef
From ffa77a246652c7e706d690682fe659f50fbe5656 Mon Sep 17 00:00:00 2001
0b49ef
From: Nils Philippsen <nils@redhat.com>
0b49ef
Date: Mon, 1 Jul 2013 12:03:51 +0200
0b49ef
Subject: [PATCH] patch: CVE-2012-4433
0b49ef
0b49ef
Squashed commit of the following:
0b49ef
0b49ef
commit 2a9071e2dc4cfe1aaa7a726805985281936f9874
0b49ef
Author: Nils Philippsen <nils@redhat.com>
0b49ef
Date:   Tue Oct 16 16:57:37 2012 +0200
0b49ef
0b49ef
    ppm-load: bring comment in line with reality
0b49ef
0b49ef
    (cherry picked from commit 6975a9cfeaf0698b42ac81b1c2f00d13c8755453)
0b49ef
0b49ef
commit 8bb88ebf78e54837322d3be74688f98800e9f33a
0b49ef
Author: Nils Philippsen <nils@redhat.com>
0b49ef
Date:   Tue Oct 16 16:56:40 2012 +0200
0b49ef
0b49ef
    ppm-load: CVE-2012-4433: add plausibility checks for header fields
0b49ef
0b49ef
    Refuse values that are non-decimal, negative or overflow the target
0b49ef
    type.
0b49ef
0b49ef
    (cherry picked from commit 4757cdf73d3675478d645a3ec8250ba02168a230)
0b49ef
0b49ef
commit 2b099886969bf055a8635d06a4d89f20fed1ee42
0b49ef
Author: Nils Philippsen <nils@redhat.com>
0b49ef
Date:   Tue Oct 16 16:58:27 2012 +0200
0b49ef
0b49ef
    ppm-load: CVE-2012-4433: don't overflow memory allocation
0b49ef
0b49ef
    Carefully selected width/height values could cause the size of a later
0b49ef
    allocation to overflow, resulting in a buffer much too small to store
0b49ef
    the data which would then written beyond its end.
0b49ef
0b49ef
    (cherry picked from commit 1e92e5235ded0415d555aa86066b8e4041ee5a53)
0b49ef
---
0b49ef
 operations/external/ppm-load.c | 64 +++++++++++++++++++++++++++++++++++-------
0b49ef
 1 file changed, 54 insertions(+), 10 deletions(-)
0b49ef
0b49ef
diff --git a/operations/external/ppm-load.c b/operations/external/ppm-load.c
0b49ef
index efe6d56..e22521c 100644
0b49ef
--- a/operations/external/ppm-load.c
0b49ef
+++ b/operations/external/ppm-load.c
0b49ef
@@ -36,6 +36,7 @@ gegl_chant_file_path (path, _("File"), "", _("Path of file to load."))
0b49ef
 #include "gegl-chant.h"
0b49ef
 #include <stdio.h>
0b49ef
 #include <stdlib.h>
0b49ef
+#include <errno.h>
0b49ef
 
0b49ef
 typedef enum {
0b49ef
   PIXMAP_ASCII  = 51,
0b49ef
@@ -44,8 +45,8 @@ typedef enum {
0b49ef
 
0b49ef
 typedef struct {
0b49ef
 	map_type   type;
0b49ef
-	gint       width;
0b49ef
-	gint       height;
0b49ef
+	glong      width;
0b49ef
+	glong      height;
0b49ef
         gsize      numsamples; /* width * height * channels */
0b49ef
         gsize      bpc;        /* bytes per channel */
0b49ef
 	guchar    *data;
0b49ef
@@ -61,7 +62,7 @@ ppm_load_read_header(FILE       *fp,
0b49ef
     gchar  header[MAX_CHARS_IN_ROW];
0b49ef
     gint   maxval;
0b49ef
 
0b49ef
-    /* Check the PPM file Type P2 or P5 */
0b49ef
+    /* Check the PPM file Type P3 or P6 */
0b49ef
     fgets (header,MAX_CHARS_IN_ROW,fp);
0b49ef
 
0b49ef
     if (header[0] != ASCII_P ||
0b49ef
@@ -82,12 +83,33 @@ ppm_load_read_header(FILE       *fp,
0b49ef
       }
0b49ef
 
0b49ef
     /* Get Width and Height */
0b49ef
-    img->width  = strtol (header,&ptr,0);
0b49ef
-    img->height = atoi (ptr);
0b49ef
-    img->numsamples = img->width * img->height * CHANNEL_COUNT;
0b49ef
+    errno = 0;
0b49ef
+    img->width  = strtol (header,&ptr,10);
0b49ef
+    if (errno)
0b49ef
+      {
0b49ef
+        g_warning ("Error reading width: %s", strerror(errno));
0b49ef
+        return FALSE;
0b49ef
+      }
0b49ef
+    else if (img->width < 0)
0b49ef
+      {
0b49ef
+        g_warning ("Error: width is negative");
0b49ef
+        return FALSE;
0b49ef
+      }
0b49ef
+
0b49ef
+    img->height = strtol (ptr,&ptr,10);
0b49ef
+    if (errno)
0b49ef
+      {
0b49ef
+        g_warning ("Error reading height: %s", strerror(errno));
0b49ef
+        return FALSE;
0b49ef
+      }
0b49ef
+    else if (img->width < 0)
0b49ef
+      {
0b49ef
+        g_warning ("Error: height is negative");
0b49ef
+        return FALSE;
0b49ef
+      }
0b49ef
 
0b49ef
     fgets (header,MAX_CHARS_IN_ROW,fp);
0b49ef
-    maxval = strtol (header,&ptr,0);
0b49ef
+    maxval = strtol (header,&ptr,10);
0b49ef
 
0b49ef
     if ((maxval != 255) && (maxval != 65535))
0b49ef
       {
0b49ef
@@ -109,6 +131,16 @@ ppm_load_read_header(FILE       *fp,
0b49ef
       g_warning ("%s: Programmer stupidity error", G_STRLOC);
0b49ef
     }
0b49ef
 
0b49ef
+    /* Later on, img->numsamples is multiplied with img->bpc to allocate
0b49ef
+     * memory. Ensure it doesn't overflow. */
0b49ef
+    if (!img->width || !img->height ||
0b49ef
+        G_MAXSIZE / img->width / img->height / CHANNEL_COUNT < img->bpc)
0b49ef
+      {
0b49ef
+        g_warning ("Illegal width/height: %ld/%ld", img->width, img->height);
0b49ef
+        return FALSE;
0b49ef
+      }
0b49ef
+    img->numsamples = img->width * img->height * CHANNEL_COUNT;
0b49ef
+
0b49ef
     return TRUE;
0b49ef
 }
0b49ef
 
0b49ef
@@ -229,12 +261,24 @@ process (GeglOperation       *operation,
0b49ef
   if (!ppm_load_read_header (fp, &img))
0b49ef
     goto out;
0b49ef
 
0b49ef
-  rect.height = img.height;
0b49ef
-  rect.width = img.width;
0b49ef
-
0b49ef
   /* Allocating Array Size */
0b49ef
+
0b49ef
+  /* Should use g_try_malloc(), but this causes crashes elsewhere because the
0b49ef
+   * error signalled by returning FALSE isn't properly acted upon. Therefore
0b49ef
+   * g_malloc() is used here which aborts if the requested memory size can't be
0b49ef
+   * allocated causing a controlled crash. */
0b49ef
   img.data = (guchar*) g_malloc (img.numsamples * img.bpc);
0b49ef
 
0b49ef
+  /* No-op without g_try_malloc(), see above. */
0b49ef
+  if (! img.data)
0b49ef
+    {
0b49ef
+      g_warning ("Couldn't allocate %" G_GSIZE_FORMAT " bytes, giving up.", ((gsize)img.numsamples * img.bpc));
0b49ef
+      goto out;
0b49ef
+    }
0b49ef
+
0b49ef
+  rect.height = img.height;
0b49ef
+  rect.width = img.width;
0b49ef
+
0b49ef
   switch (img.bpc)
0b49ef
     {
0b49ef
     case 1:
0b49ef
-- 
0b49ef
1.8.3.1
0b49ef