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