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