Definition:
Heap Corruption occurs when a thread allocates a block of heap memory of a given size and then writes to memory
addresses beyond the requested size of the heap block. Another common cause of heap corruption is writing to block
of memory that has been freed or freeing a block of memory already freed.
In this scenario, we will try to demonstrate heap corruption caused by reusing a block of memory already freed (Old Pointer Reuse).
1) Without Page Heap Enabled.
The faulting Stack
First, we need to add the Private symbol path(company wide symbols) as well as symbols of our Filter
a) Using the GUI:
- Click "File"
- Click "Symbol File Path"
- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pri and hit Enter
b) Using the ".sympath+" command:
0:008> .sympath+ SRV*C:\LabSymbols*\\symbols\symbols
Symbol search path is: SRV*C:\LabSymbols*\\symbols\symbols
0:008> .sympath+ C :\IISDebugLabs\symbols\Pri (where private symbols of badFil.dll are located)
Symbol search path is:SRV*C:\LabSymbols* \\symbols\symbols; C :\IISDebugLabs\symbols\Pub
Note: For more information on setting the symbol path for windbg, please refer to the debugger documentation for the .sympath command. This will describe
the "downstream store" feature that lets you download symbols to a symbol server of your own.
The stack backtrace of the thread that AV'd would look like:
0:015> kvn
# ChildEBP RetAddr Args to Child
00 0121f6e8 77a9d751 00070000 00000000 00000010 ntdll!RtlAllocateHeap+0x5b4 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 1818]
01 0121f73c 77a8296a 000b1acc 000b1ac8 000b1b38 ole32!CStdMarshal::DisconnectCliIPIDs+0x8b (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\ole32\com\dcomrem\marshal.cxx @ 3472]
02 0121f764 77a826a1 00000002 000b1b5c 000b1ac8 ole32!CStdMarshal::Disconnect+0x19c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\ole32\com\dcomrem\marshal.cxx @ 3096]
03 0121f784 77a82614 000b1ac8 77aa1ec4 00000001 ole32!CStdIdentity::~CStdIdentity+0x89 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\ole32\com\dcomrem\stdid.cxx @ 297]
04 0121f78c 77aa1ec4 00000001 65d712f8 000c3a9c ole32!CStdIdentity::`scalar deleting destructor'+0x8 (FPO: [1,0,1]) (CONV: thiscall)
05 0121f7a0 77d94442 80000000 77d493be 000c3a9c ole32!CStdIdentity::CInternalUnk::Release+0x4c (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\stdid.cxx @ 736]
06 0121f7a8 77d493be 000c3a9c 77d938c9 0121f8b4 RPCRT4!IUnknown_Release_Proxy+0xd (FPO: [1,0,0]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\proxy.c @ 1797]
07 0121f7b0 77d938c9 0121f8b4 000c3a9c 65d712f8 RPCRT4!NdrInterfacePointerFree+0xe (FPO: [3,0,0]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\free.c @ 1515]
08 0121f7d4 77d937de 0121f8b4 00000005 65d71252 RPCRT4!NdrpFreeParams+0x69 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1967]
09 0121fa54 77d949ac 0009cf10 0009e0b8 000c6050 RPCRT4!NdrStubCall2+0x8cf (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1869]
0a 0121fab8 77b29e7f 0009cf10 000c6050 0009e0b8 RPCRT4!CStdStubBuffer_Invoke+0xec (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\stub.c @ 1571]
0b 0121fafc 77b29d6e 000c6050 000ad00c 0009f0f0 ole32!SyncStubInvoke+0x61 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1136]
0c 0121fb44 77aa7ae7 000c6050 000acf20 0009cf10 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]
0d 0121fba8 77aa7a33 0009e0b8 00000000 0009cf10 ole32!CCtxComChnl::ContextInvoke+0xbb (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 1204]
0e 0121fbc4 77b29c89 000c6050 00000001 0009cf10 ole32!MTAInvoke+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 1952]
0f 0121fbf4 77b29a89 000c6008 0009e0b8 0009cf10 ole32!AppInvoke+0xb5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1013]
10 0121fcb4 77b2a12c 000cfb30 00000000 000acff0 ole32!ComInvokeWithLockAndIPID+0x29e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1578]
11 0121fcf4 77d33721 000c6008 000acff0 00079af0 ole32!ThreadInvoke+0x1b7 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 3835]
12 0121fd2c 77d33667 77b29f7b 00079af0 0121fe0c RPCRT4!DispatchToStubInC+0x84 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\dispatch.c @ 82]
13 0121fd84 77d33579 00000000 00000000 0121fe0c RPCRT4!RPC_INTERFACE::DispatchToStubWorker+0x100 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 543]
The faulting stack in a heap corruption issue will involve heap operations such as the one here " RtlAllocateHeap() " The first parameter passed to the finction is the
heap address.
Let see hoe heap " 00070000 " looks like:
0:015> !heap 00070000 -m
Index Address Name Debugging options enabled
1: 00070000
Segment at 00070000 to 00170000 (00061000 bytes committed)
Flags: 00000002
ForceFlags: 00000000
Granularity: 8 bytes
Segment Reserve: 00100000
Segment Commit: 00002000
DeCommit Block Thres:00000200
DeCommit Total Thres:00002000
Total Free Size: fffffffe Max.
Allocation Size:7ffdefff
Lock Variable at: 00070608
Next TagIndex: 0000
Maximum TagIndex: 0000
Tag Entries: 00000000
PsuedoTag Entries: 00000000
Virtual Alloc List: 00070050
UCR FreeList: 00070598
FreeList Usage: 00000040 00000000 00000000 00000000
FreeList[ 00 ] at 00070178: 000d0348 . 000d0348
FreeList[ 02 ] at 00070188: 00070189 . 00070188
FreeList[ 05 ] at 000701a0: 000801a0 . 00070003
Unabled to read HEAP_ENTRY structure at 0000fff8
FreeList[ 08 ] at 000701b8: 000700a0 . 0003019d
Unabled to read HEAP_ENTRY structure at 00030195
Segment00 at 00070640:
Flags: 00000000
Base: 00070000
First Entry: 00070680
Last Entry: 00170000
Total Pages: 00000100
Total UnCommit: 0000009f
Largest UnCommit:0009f000
UnCommitted Ranges: (1)
000d1000: 0009f000
So what this tells us is that the heap at " 00070000 " is corrupted. The thread displayed above was trying to allocate some memory (58 B) in that heap
but it was unable to do so since the heap is corrupted and hence it AV'd. Which means that the thread is either a victim of heap corruption
(some other thread corrupted the heap and this one was caught while doing some heap operations in that specific heap address) or it might
be that this thread corrupted the heap previously. So basically, the only conclusion we could get from this is that this is a heap corruption
problem and we can't identify the cause of this corruption just by analyzing this crash.
To know who corrupted the heap, we need to Enable Page Heap (A memory management feature to track more closely all heap operations).
This second of this debug annotation shows how to do that and identify the culprit.
2) With Page Heap Enabled.
a) Turn On "Enable Page Heap"
- Go to the Command Prompt
- Switch to the folder where the debugging tools for windows is installed
- Type in "GFlags" and hit Enter
- In the "Image File Name", Enter "dllhost.exe" without quotes
- Click "Image File Options" radio button
- Click "Enable Page Heap" check box
- Click "Apply"
- Click "OK"
- Restart IIS
b) Run the scenario again
- Do Step 3 and 4 in the "Lab Setup" Section
- Click "Corrupt Heap" one time
- Note that the debugger breaks and diplays
a- Comments about the exception
b- The value of the registers
c- The module where the AV happened
d- The assembly line that caused the AV
- In the WinDbg command line, type in "g" and press "Enter", a 2nd chance AV is now encountered
- Notice the "Page Heap" Debug output.
c) Line up the Private Symbols
- Point the debugger to use the private symbols
- For company wide internal symbols, use \\symbols\symbols
- For the component private symbols, use C:\IISDebugLabs\Symbols\Pri
The faulting Stack
0:001> g
Page heap: block @ 02C02FF0 is corrupted (reason 27)
Page heap: reason: corrupted start stamp
Page heap: reason: corrupted end stamp
Page heap: reason: corrupted heap pointer
Page heap: reason: raised exception while probing
(4c8.784): Break instruction exception - code 80000003 (first chance)
eax=00000000 ebx=00000027 ecx=77fb3d26 edx=00c9f2ae esi=02c02fd0 edi=00511000
eip=77f97704 esp=00c9f49c ebp=00c9f4c8 iopl=0 nv up ei pl zr na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
ntdll!DbgBreakPoint:
77f97704 cc int 3
This shows the debug output of "Page Heap" showing the attempt of corrupting the heap by the thread shown blow.
The stack backtrace of the thread that AV'd would look like:
0:001> kvn
# ChildEBP RetAddr Args to Child
00 00c9f498 77fb3de6 02c02ff0 77fb4248 02c02ff0 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]
01 00c9f4a0 77fb4248 02c02ff0 00000027 00511000 ntdll!RtlpDphReportCorruptedBlock+0x8c (FPO: [2,0,1]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4142]
02 00c9f4c8 77fb2f3f 00511000 01001002 02c02ff0 ntdll!RtlpDphNormalHeapFree+0x46 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4575]
03 00c9f4ec 77fb5887 00510000 01001002 02c02ff0 ntdll!RtlpDebugPageHeapFree+0xa6 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 2862]
04 00c9f564 77fa7111 00510000 01001002 02c02ff0 ntdll!RtlDebugFreeHeap+0x203 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 915]
05 00c9f604 77fcbeda 00510000 01001002 02c02ff0 ntdll!RtlFreeHeapSlowly+0x4d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3466]
06 00c9f6a8 7800115c 00510000 00000000 02c02ff0 ntdll!RtlFreeHeap+0x53 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3138]
07 00c9f6f0 02cb1720 02c02ff0 00587ff0 78002041 MSVCRT!free+0xda (CONV: cdecl) [free.c @ 101]
08 00c9f708 02cb142f 022e6d70 00000001 022e6e3c badext!doHC3+0x2c (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 319]
09 00c9f728 65d82b06 022e6d70 022e6e3c 022e6d70 badext!HttpExtensionProc+0x148 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 123]
0a 00c9f73c 65d82a97 022e6d70 022e6e3c 022e6d70 wam!HSE_APPDLL::ExecuteRequest+0x93 (FPO: [1,0,3]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 450]
0b 00c9f770 65d823a2 00569f70 022e6ef2 022e6d70 wam!WAM::InvokeExtension+0x35 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 913]
0c 00c9f7b4 77d339c0 005f3f80 00000000 000000c8 wam!WAM::ProcessRequest+0x240 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 643]
0d 00c9f7dc 77d93570 65d8223f 00c9f7f8 00000005 RPCRT4!Invoke+0x30 [..\i386\stubless.asm @ 88]
0e 00c9fa54 77d949ac 01418fe0 01a66e98 019f4f28 RPCRT4!NdrStubCall2+0x63d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1724]
0f 00c9fab8 77b29e7f 01418fe0 019f4f28 01a66e98 RPCRT4!CStdStubBuffer_Invoke+0xec (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\stub.c @ 1571]
10 00c9fafc 77b29d6e 019f4f28 019d2f24 019e0aa8 ole32!SyncStubInvoke+0x61 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1136]
11 00c9fb44 77aa7ae7 019f4f28 019c6f38 01418fe0 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]
12 00c9fba8 77aa7a33 01a66e98 00000000 01418fe0 ole32!CCtxComChnl::ContextInvoke+0xbb (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 1204]
13 00c9fbc4 77b29c89 019f4f28 00000001 01418fe0 ole32!MTAInvoke+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 1952]
So basically, this is the thread that corrupted the heap, It's shows now that the culprit is "badFill.dll".
Notice also the function call "doHC2" as well as the line of code in "badfil.cpp"
Verify the filter's symbols are lined up.
0:001> lmv mbadext
start end module name
02920000 02926000 badext (pdb symbols) c:\iisdebuglabs\symbols\pri\badext.pdb
Loaded symbol image file: C:\IISDebugLabs\www\app\badext.dll
Image path: C:\IISDebugLabs\www\app\badext.dll
Timestamp: Sat Jun 21 16:11:39 2003 (3EF4BBFB)
Checksum: 00008518
File version: 5.0.0.0
Product version: 5.0.0.0
File flags: 0 (Mask 3F)
File OS: 40004 NT Win32
File type: 2.0 Dll
File date: 00000000.00000000
CompanyName: Microsoft Corporation
ProductName: Internet Information Services
InternalName: badEXT.dll
OriginalFilename: badEXT.dll
ProductVersion: 5.00.0984
FileVersion: 5.00.0984
FileDescription: badEXT Test
LegalCopyright: Copyright (C) Microsoft Corp. 1981-1999
Line 319 looks like
DWORD doHC3(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) //Old Pointer reuse
{
if (1 == Variation)
{
char *ptr, *tmp;
int i;
ptr = (char*)GlobalAlloc(GMEM_FIXED, 16);
tmp = ptr;
for (i = 0; i < 16; ++i)
*(tmp++) = 'b';
free(ptr); //line 319
*ptr = 'x';
return (DWORD) ptr;
}
...
}