Exploit-DB #40257
Description: Microsoft Windows - GDI+ EMR_EXTTEXTOUTA / EMR_POLYTEXTOUTA Heap Buffer Overflow (MS16-097)
Type: dos
Platform: windows
Date: 2016-08-17
Author: Google Security Research

Exploit:
Source: https://bugs.chromium.org/p/project-zero/issues/detail?id=828

The Microsoft GDI+ implementation of the EMF format supports records corresponding to the ExtTextOutA() and PolyTextOutA() API functions. Both CEmfPlusEnumState::ExtTextOutA and CEmfPlusEnumState::PolyTextOutA handlers suffer from a security vulnerability in the handling of the "offDx" record field, which is described in the following way in the format specification:

  --- cut ---
  offDx (4 bytes): A 32-bit unsigned integer that specifies the offset to an intercharacter spacing
  array, in bytes, from the start of the record in which this object is contained. This value MUST be
  32-bit aligned.
  --- cut ---

The offset is supposed to address an array of "Chars" (another field in the text records, specifying the number of characters to be displayed) double words, taking up a total of 4 * N bytes. However, instead of verifying that the provided record is sufficiently large to contain 4 * N bytes at the specified offset, it only checks if it can fit 4 bytes (completely ignoring the actual number of characters in the message, which can be larger than 1). A pseudo-code of the current, vulnerable code is shown below:

  --- cut ---
  if ( record_size - offString >= nChars && (!nChars || record_size - 4 >= record->emrtext.offDx) ) {
    // Validation passed, continue processing the record.
  }
  --- cut ---

There is definitely a flaw in the implementation, but one which would typically only lead to an out-of-bound read condition, since it's a problem with the sanitization of an input buffer. However, the logic found in the remainder of the function is as follows:

- Attempt to convert the textual ANSI string in the record to a wide-char string, using the MultiByteToWideChar() function and the code page specified in the most recently selected font.
  - If the number of characters converted is equal to the number of bytes in the input buffer, CEmfPlusEnumState::PlayExtTextOut() is called and the function returns.
  - Otherwise, the function proceeds to rewrite the offDx buffer by calling EmfEnumState::CreateCopyOfCurrentRecord() to allocate an exact copy of the current record (with the same size), and then copying entries of the intercharacter spacing array, omitting those corresponding to bytes which cause the IsDBCSLeadByteEx() function to return true. Once the rewriting is performed, CEmfPlusEnumState::PlayExtTextOut() is called with the new record as the parameter.

In order to trigger the more interesting array rewriting behavior, we must get the MultiByteToWideChar() function to convert fewer characters than there are bytes in the input buffer, which means we have to utilize a string in a non-standard encoding, which supports double-byte character sets (DBCS). Luckily, this is possible by selecting a font with an appropriate charset (e.g. SHIFTJIS_CHARSET) into the HDC, and invoking either of the *TextOutA() handlers with a byte stream containing so-called lead bytes (which folds two bytes into a single character, decreasing the eventual return value of the MultiByteToWideChar() call).

Since the spacing array in the new record is too small to store entries for all "Chars" characters, it is overflown with data read from memory after the original record buffer. Considering the complexity of the EMF format, other records in the picture file could be easily used to massage the heap such that the record copy is overflown with fully controlled data. The issue has been reproduced in Microsoft Office 2013, as well as a simple C++ program which boils down to the following calls:

  --- cut ---
  Graphics graphics(hdc);
  Metafile *mf = new Metafile(L"C:\\path\\to\\poc.emf");

  INT conversionSuccess;
  mf->ConvertToEmfPlus(&graphics, &conversionSuccess, Gdiplus::EmfTypeEmfPlusDual, NULL);
  --- cut ---

An example crash log from PowerPoint 2013, indicating heap corruption, is shown below (the condition can also be reproduced reliably by enabling Page Heap on the GDI+ client process):

  --- cut ---
  (2a8c.2bd8): Break instruction exception - code 80000003 (first chance)
  eax=00000000 ebx=00000000 ecx=772336ab edx=0022cb85 esi=03bd0000 edi=1171ffc0
  eip=7728e815 esp=0022cdd8 ebp=0022ce50 iopl=0         nv up ei pl nz na pe nc
  cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200206
  ntdll!RtlReportCriticalFailure+0x29:
  7728e815 cc              int     3
  0:000> kb
  ChildEBP RetAddr  Args to Child              
  0022ce50 7728f749 c0000374 772c4270 0022ce94 ntdll!RtlReportCriticalFailure+0x29
  0022ce60 7728f[PII_REDACTED]2 64dc1326 03bd0000 ntdll!RtlpReportHeapFailure+0x21
  0022ce94 7724ab46 0000000c 03bd0000 1171ffc0 ntdll!RtlpLogHeapFailure+0xa1
  0022cf84 771f3431 [PII_REDACTED] 03bd00c4 ntdll!RtlpAllocateHeap+0x7b2
  0022d008 695071ec 03bd0000 [PII_REDACTED] ntdll!RtlAllocateHeap+0x23a
  0022d01c 6951bbf1 00000258 116b5104 03bdd558 gdiplus!GpMalloc+0x16
  0022d[PII_REDACTED]5 116b50e0 116b50e0 03bdd558 gdiplus!GpGraphics::Save+0x11
  0022d4b0 69557bdc 116b50e0 116b5104 116b30d8 gdiplus!CEmfPlusEnumState::PlayExtTextOut+0xda
  0022d4ec 69557f25 00000053 03bdae00 00006044 gdiplus!CEmfPlusEnumState::ExtTextOutA+0x136
  0022d500 695286ca [PII_REDACTED] 0d67b568 gdiplus!CEmfPlusEnumState::ProcessRecord+0x13b
  0022d51c [PII_REDACTED] [PII_REDACTED] gdiplus!GdipPlayMetafileRecordCallback+0x6c
  0022d544 768155f4 9d211b17 0d567180 0d67b568 gdiplus!EnumEmfDownLevel+0x6e
  0022d5d0 6952aa36 9d211b17 403581b3 695287f4 GDI32!bInternalPlayEMF+0x6a3
  0022d608 6952d199 9d211b17 05[PII_REDACTED]d674 gdiplus!MetafilePlayer::EnumerateEmfRecords+0x104
  0022d6b0 6952f455 [PII_REDACTED] 0022d7d8 gdiplus!GpGraphics::EnumEmf+0x391
  0022d810 [PII_REDACTED] 42[PII_REDACTED]1d0b gdiplus!GpMetafile::EnumerateForPlayback+0x7b9
  0022d90c 695347c6 03bd2fd8 [PII_REDACTED] gdiplus!GpGraphics::DrawImage+0x3f5
  0022d970 6952c792 03bd2fd8 0022