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