CVE-2018-16644: Improper Restriction of Operations within the Bounds of a Memory Buffer (CWE-119)
value: There is a missing check for length in the functions ReadDCMImage of coders/dcm.c and ReadPICTImage of coders/pict.c in ImageMagick 7.0.8-11, which allows remote attackers to cause a denial of service via a crafted image.
CWE description: The product performs operations on a memory buffer, but it reads from or writes to a memory location outside the buffer's intended boundary. This may result in read or write operations on unexpected memory locations that could be linked to other variables, data structures, or internal program data.
CVSS v3 base score: 6.5
Published: 2018-09-06T22:29Z
Repository: https://github.com/ImageMagick/ImageMagick
Language: C
Files changed: coders/pict.c
Commit date: 2018-08-27 19:35:15 -0400
Commit message: https://github.com/ImageMagick/ImageMagick/issues/1269

Diff: diff --git a/coders/pict.c b/coders/pict.c
index 35dadfeee0f..3600c121525 100644
--- a/coders/pict.c
+++ b/coders/pict.c
@@ -980,10 +980,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
           {
             /*
               Clipping rectangle.
             */
             length=ReadBlobMSBShort(image);
+            if (length > GetBlobSize(image))
+              ThrowPICTException(CorruptImageError,
+                "InsufficientImageDataInFile");
             if (length != 0x000a)
               {
                 for (i=0; i < (ssize_t) (length-2); i++)
                   if (ReadBlobByte(image) == EOF)
                     break;
@@ -1028,10 +1031,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
                 break;
               }
             if (pattern != 1)
               ThrowPICTException(CorruptImageError,"UnknownPatternType");
             length=ReadBlobMSBShort(image);
+            if (length > GetBlobSize(image))
+              ThrowPICTException(CorruptImageError,
+                "InsufficientImageDataInFile");
             if (ReadRectangle(image,&frame) == MagickFalse)
               ThrowPICTException(CorruptImageError,"ImproperImageHeader");
             if (ReadPixmap(image,&pixmap) == MagickFalse)
               ThrowPICTException(CorruptImageError,"ImproperImageHeader");
             image->depth=(size_t) pixmap.component_size;
@@ -1039,10 +1045,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
             image->resolution.y=1.0*pixmap.vertical_resolution;
             image->units=PixelsPerInchResolution;
             (void) ReadBlobMSBLong(image);
             flags=(ssize_t) ReadBlobMSBShort(image);
             length=ReadBlobMSBShort(image);
+            if (length > GetBlobSize(image))
+              ThrowPICTException(CorruptImageError,
+                "InsufficientImageDataInFile");
             for (i=0; i <= (ssize_t) length; i++)
               (void) ReadBlobMSBLong(image);
             width=(size_t) (frame.bottom-frame.top);
             height=(size_t) (frame.right-frame.left);
             if (pixmap.bits_per_pixel <= 8)
@@ -1099,10 +1108,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
           {
             /*
               Skip polygon or region.
             */
             length=ReadBlobMSBShort(image);
+            if (length > GetBlobSize(image))
+              ThrowPICTException(CorruptImageError,
+                "InsufficientImageDataInFile");
             for (i=0; i < (ssize_t) (length-2); i++)
               if (ReadBlobByte(image) == EOF)
                 break;
             break;
           }
@@ -1221,10 +1233,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
               {
                 /*
                   Skip region.
                 */
                 length=ReadBlobMSBShort(image);
+                if (length > GetBlobSize(image))
+                  ThrowPICTException(CorruptImageError,
+                    "InsufficientImageDataInFile");
                 for (i=0; i < (ssize_t) (length-2); i++)
                   if (ReadBlobByte(image) == EOF)
                     break;
               }
             if ((code != 0x9a) && (code != 0x9b) &&
@@ -1343,10 +1358,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
             /*
               Comment.
             */
             type=ReadBlobMSBShort(image);
             length=ReadBlobMSBShort(image);
+            if (length > GetBlobSize(image))
+              ThrowPICTException(CorruptImageError,
+                "InsufficientImageDataInFile");
             if (length == 0)
               break;
             (void) ReadBlobMSBLong(image);
             length-=MagickMin(length,4);
             if (length == 0)
@@ -1452,10 +1470,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
             (void) CopyMagickString(image->filename,read_info->filename,
               MagickPathExtent);
             ThrowPICTException(FileOpenError,"UnableToCreateTemporaryFile");
           }
         length=ReadBlobMSBLong(image);
+        if (length > GetBlobSize(image))
+          ThrowPICTException(CorruptImageError,
+            "InsufficientImageDataInFile");
         if (length > 154)
           {
             for (i=0; i < 6; i++)
               (void) ReadBlobMSBLong(image);
             if (ReadRectangle(image,&frame) == MagickFalse)
@@ -1503,10 +1524,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
       {
         /*
           Skip reserved.
         */
         length=ReadBlobMSBShort(image);
+        if (length > GetBlobSize(image))
+          ThrowPICTException(CorruptImageError,
+            "InsufficientImageDataInFile");
         for (i=0; i < (ssize_t) length; i++)
           if (ReadBlobByte(image) == EOF)
             break;
         continue;
       }
@@ -1514,10 +1538,13 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
       {
         /*
           Skip reserved.
         */
         length=(size_t) ((code >> 7) & 0xff);
+        if (length > GetBlobSize(image))
+          ThrowPICTException(CorruptImageError,
+            "InsufficientImageDataInFile");
         for (i=0; i < (ssize_t) length; i++)
           if (ReadBlobByte(image) == EOF)
             break;
         continue;
       }

Fixed code:             if (length > GetBlobSize(image))
              ThrowPICTException(CorruptImageError,
                "InsufficientImageDataInFile");
            if (length > GetBlobSize(image))
              ThrowPICTException(CorruptImageError,
                "InsufficientImageDataInFile");
            if (length > GetBlobSize(image))
              ThrowPICTException(CorruptImageError,
                "InsufficientImageDataInFile");
            if (length > GetBlobSize(image))
              ThrowPICTException(CorruptImageError,
                "InsufficientImageDataInFile");
                if (length > GetBlobSize(image))
                  ThrowPICTException(CorruptImageError,
                    "InsufficientImageDataInFile");
            if (length > GetBlobSize(image))
              ThrowPICTException(CorruptImageError,
                "InsufficientImageDataInFile");
        if (length > GetBlobSize(image))
          ThrowPICTException(CorruptImageError,
            "InsufficientImageDataInFile");
        if (length > GetBlobSize(image))
          ThrowPICTException(CorruptImageError,
            "InsufficientImageDataInFile");
        if (length > GetBlobSize(image))
          ThrowPICTException(CorruptImageError,
            "InsufficientImageDataInFile");