Blame SOURCES/libjpeg-turbo-CVE-2018-14498.patch

e8d57f
From c7dd3cd0fec2d6785f2bd79e3e2f0adb62ee8bc1 Mon Sep 17 00:00:00 2001
e8d57f
From: DRC <information@libjpeg-turbo.org>
e8d57f
Date: Fri, 20 Jul 2018 17:21:36 -0500
e8d57f
Subject: [PATCH] cjpeg: Fix OOB read caused by malformed 8-bit BMP
e8d57f
e8d57f
... in which one or more of the color indices is out of range for the
e8d57f
number of palette entries.
e8d57f
e8d57f
Fix partly borrowed from jpeg-9c.  This commit also adopts Guido's
e8d57f
JERR_PPM_OUTOFRANGE enum value in lieu of our project-specific
e8d57f
JERR_PPM_TOOLARGE enum value.
e8d57f
e8d57f
Fixes #258
e8d57f
---
e8d57f
 cderror.h |  5 +++--
e8d57f
 rdbmp.c   |  7 ++++++-
e8d57f
 rdppm.c   | 12 ++++++------
e8d57f
 3 files changed, 15 insertions(+), 9 deletions(-)
e8d57f
e8d57f
diff --git a/cderror.h b/cderror.h
e8d57f
index 63de498..e57a8c8 100644
e8d57f
--- a/cderror.h
e8d57f
+++ b/cderror.h
e8d57f
@@ -2,7 +2,7 @@
e8d57f
  * cderror.h
e8d57f
  *
e8d57f
  * Copyright (C) 1994-1997, Thomas G. Lane.
e8d57f
- * Modified 2009 by Guido Vollbeding.
e8d57f
+ * Modified 2009-2017 by Guido Vollbeding.
e8d57f
  * This file is part of the Independent JPEG Group's software.
e8d57f
  * For conditions of distribution and use, see the accompanying README.ijg
e8d57f
  * file.
e8d57f
@@ -49,6 +49,7 @@ JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
e8d57f
 JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
e8d57f
 JMESSAGE(JERR_BMP_EMPTY, "Empty BMP image")
e8d57f
 JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
e8d57f
+JMESSAGE(JERR_BMP_OUTOFRANGE, "Numeric value out of range in BMP file")
e8d57f
 JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
e8d57f
 JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
e8d57f
 JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
e8d57f
@@ -75,8 +76,8 @@ JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
e8d57f
 #ifdef PPM_SUPPORTED
e8d57f
 JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
e8d57f
 JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
e8d57f
-JMESSAGE(JERR_PPM_TOOLARGE, "Integer value too large in PPM file")
e8d57f
 JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
e8d57f
+JMESSAGE(JERR_PPM_OUTOFRANGE, "Numeric value out of range in PPM file")
e8d57f
 JMESSAGE(JTRC_PGM, "%ux%u PGM image")
e8d57f
 JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
e8d57f
 JMESSAGE(JTRC_PPM, "%ux%u PPM image")
e8d57f
diff --git a/rdbmp.c b/rdbmp.c
e8d57f
index 4104b68..a7dbe9f 100644
e8d57f
--- a/rdbmp.c
e8d57f
+++ b/rdbmp.c
e8d57f
@@ -3,7 +3,7 @@
e8d57f
  *
e8d57f
  * This file was part of the Independent JPEG Group's software:
e8d57f
  * Copyright (C) 1994-1996, Thomas G. Lane.
e8d57f
- * Modified 2009-2010 by Guido Vollbeding.
e8d57f
+ * Modified 2009-2017 by Guido Vollbeding.
e8d57f
  * libjpeg-turbo Modifications:
e8d57f
  * Modified 2011 by Siarhei Siamashka.
e8d57f
  * Copyright (C) 2015, D. R. Commander.
e8d57f
@@ -66,6 +66,7 @@ typedef struct _bmp_source_struct {
e8d57f
   JDIMENSION row_width;         /* Physical width of scanlines in file */
e8d57f
 
e8d57f
   int bits_per_pixel;           /* remembers 8- or 24-bit format */
e8d57f
+  int cmap_length;              /* colormap length */
e8d57f
 } bmp_source_struct;
e8d57f
 
e8d57f
 
e8d57f
@@ -126,6 +127,7 @@ get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
e8d57f
 {
e8d57f
   bmp_source_ptr source = (bmp_source_ptr) sinfo;
e8d57f
   register JSAMPARRAY colormap = source->colormap;
e8d57f
+  int cmaplen = source->cmap_length;
e8d57f
   JSAMPARRAY image_ptr;
e8d57f
   register int t;
e8d57f
   register JSAMPROW inptr, outptr;
e8d57f
@@ -142,6 +144,8 @@ get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
e8d57f
   outptr = source->pub.buffer[0];
e8d57f
   for (col = cinfo->image_width; col > 0; col--) {
e8d57f
     t = GETJSAMPLE(*inptr++);
e8d57f
+    if (t >= cmaplen)
e8d57f
+      ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
e8d57f
     *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */
e8d57f
     *outptr++ = colormap[1][t];
e8d57f
     *outptr++ = colormap[2][t];
e8d57f
@@ -401,6 +405,7 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
e8d57f
     source->colormap = (*cinfo->mem->alloc_sarray)
e8d57f
       ((j_common_ptr) cinfo, JPOOL_IMAGE,
e8d57f
        (JDIMENSION) biClrUsed, (JDIMENSION) 3);
e8d57f
+    source->cmap_length = (int)biClrUsed;
e8d57f
     /* and read it from the file */
e8d57f
     read_colormap(source, (int) biClrUsed, mapentrysize);
e8d57f
     /* account for size of colormap */
e8d57f
diff --git a/rdppm.c b/rdppm.c
e8d57f
index 33ff749..c0c0962 100644
e8d57f
--- a/rdppm.c
e8d57f
+++ b/rdppm.c
e8d57f
@@ -69,7 +69,7 @@ typedef struct {
e8d57f
   JSAMPROW pixrow;              /* compressor input buffer */
e8d57f
   size_t buffer_width;          /* width of I/O buffer */
e8d57f
   JSAMPLE *rescale;             /* => maxval-remapping array, or NULL */
e8d57f
-  int maxval;
e8d57f
+  unsigned int maxval;
e8d57f
 } ppm_source_struct;
e8d57f
 
e8d57f
 typedef ppm_source_struct *ppm_source_ptr;
e8d57f
@@ -119,7 +119,7 @@ read_pbm_integer (j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
e8d57f
   }
e8d57f
 
e8d57f
   if (val > maxval)
e8d57f
-    ERREXIT(cinfo, JERR_PPM_TOOLARGE);
e8d57f
+    ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
e8d57f
 
e8d57f
   return val;
e8d57f
 }
e8d57f
@@ -255,7 +255,7 @@ get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
e8d57f
     temp  = UCH(*bufferptr++) << 8;
e8d57f
     temp |= UCH(*bufferptr++);
e8d57f
     if (temp > maxval)
e8d57f
-      ERREXIT(cinfo, JERR_PPM_TOOLARGE);
e8d57f
+      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
e8d57f
     *ptr++ = rescale[temp];
e8d57f
   }
e8d57f
   return 1;
e8d57f
@@ -282,17 +282,17 @@ get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
e8d57f
     temp  = UCH(*bufferptr++) << 8;
e8d57f
     temp |= UCH(*bufferptr++);
e8d57f
     if (temp > maxval)
e8d57f
-      ERREXIT(cinfo, JERR_PPM_TOOLARGE);
e8d57f
+      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
e8d57f
     *ptr++ = rescale[temp];
e8d57f
     temp  = UCH(*bufferptr++) << 8;
e8d57f
     temp |= UCH(*bufferptr++);
e8d57f
     if (temp > maxval)
e8d57f
-      ERREXIT(cinfo, JERR_PPM_TOOLARGE);
e8d57f
+      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
e8d57f
     *ptr++ = rescale[temp];
e8d57f
     temp  = UCH(*bufferptr++) << 8;
e8d57f
     temp |= UCH(*bufferptr++);
e8d57f
     if (temp > maxval)
e8d57f
-      ERREXIT(cinfo, JERR_PPM_TOOLARGE);
e8d57f
+      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
e8d57f
     *ptr++ = rescale[temp];
e8d57f
   }
e8d57f
   return 1;
e8d57f
-- 
e8d57f
2.21.0
e8d57f