CVE-2016-10054 (C, published 2017-03-23T17:59Z)
Description: value: Buffer overflow in the WriteMAPImage function in coders/map.c in ImageMagick before 6.9.5-8 allows remote attackers to cause a denial of service (application crash) or have other unspecified impact via a crafted file.
Severity: MEDIUM (CVSS v3 base score: 7.8)
Weakness: CWE-119 \u2014 Improper Restriction of Operations within the Bounds of a Memory Buffer
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.
Repository: https://github.com/ImageMagick/ImageMagick
Files changed:
ChangeLog
coders/map.c
coders/pdb.c
coders/sixel.c
coders/tiff.c
Commit message: Prevent buffer overflow in SIXEL, PDB, MAP, and CALS coders (bug report from Donghai Zhu)


Vulnerable code:
  if (image->depth <= 8)
      *q++=(unsigned char) image->colormap[i].red;
      *q++=(unsigned char) image->colormap[i].green;
      *q++=(unsigned char) image->colormap[i].blue;
      *q++=(unsigned char) ((size_t) image->colormap[i].red >> 8);
      *q++=(unsigned char) image->colormap[i].red;
      *q++=(unsigned char) ((size_t) image->colormap[i].green >> 8);
      *q++=(unsigned char) image->colormap[i].green;
      *q++=(unsigned char) ((size_t) image->colormap[i].blue >> 8);
      *q++=(unsigned char) image->colormap[i].blue;
  packet_size=(size_t) (image->depth > 8 ? 2: 1);
    imbuf = (unsigned char *) AcquireQuantumMemory(imsx * imsy,1);
    (void) ResetMagickMemory(imbuf, background_color_index, imsx * imsy);
                dmbuf = (unsigned char *) AcquireQuantumMemory(dmsx * dmsy,1);
                (void) ResetMagickMemory(dmbuf, background_color_index, dmsx * dmsy);
                dmbuf = (unsigned char *) AcquireQuantumMemory(dmsx * dmsy,1);
                (void) ResetMagickMemory(dmbuf, background_color_index, dmsx * dmsy);
                                (void) ResetMagickMemory(imbuf + imsx * y + posision_x, color_index, repeat_count);
        if ((dmbuf = (unsigned char *) AcquireQuantumMemory(dmsx * dmsy,1)) == NULL) {
  (void) SetImageType(image,BilevelType,exception);

Fixed code:
  * Prevent buffer overflow in SIXEL, PDB, MAP, and CALS coders (bug report
    from Donghai Zhu).
  q=colormap;
  if (image->colors <= 256)
      *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
      *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].green);
      *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
      *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].red) >> 8);
      *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].red) & 0xff);
      *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].green) >> 8);
      *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].green) & 0xff);;
      *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].blue) >> 8);
      *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].blue) & 0xff);
  packet_size=(size_t) (image->depth > 8 ? 2 : 1);
  status=SetQuantumDepth(image,quantum_info,image->depth > 8 ? 16 : 8);
    imbuf = (unsigned char *) AcquireQuantumMemory(imsx , imsy);
    (void) ResetMagickMemory(imbuf, background_color_index, (size_t) imsx * imsy);
                dmbuf = (unsigned char *) AcquireQuantumMemory(dmsx , dmsy);
                (void) ResetMagickMemory(dmbuf, background_color_index, (size_t) dmsx * dmsy);
                dmbuf = (unsigned char *) AcquireQuantumMemory(dmsx , dmsy);
                (void) ResetMagickMemory(dmbuf, background_color_index, (size_t) dmsx * dmsy);
                                (void) ResetMagickMemory(imbuf + (size_t) imsx * y + posision_x, color_index, repeat_count);
        if ((dmbuf = (unsigned char *) AcquireQuantumMemory(dmsx , dmsy)) == NULL) {
  (void) SetImageType(image,BilevelType,exception);