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 overwriting to a heap block beyond the requested
size (Buffer Overwrite)
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:020> kvn
# ChildEBP RetAddr Args to Child
00 01e4f88c 77a527ca 00070000 00000000 0000011c ntdll!RtlAllocateHeap+0x361 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 2063]
01 01e4f89c 77aa723f 0000011c 000b161c 000be1d8 ole32!operator new+0x12 (FPO: [1,0,0]) (CONV: cdecl) [D:\nt\private\ole32\com\util\w32new.cxx @ 31]
02 01e4f8b0 77b2a2c9 00000001 01e4f8fc 000b161c ole32!GetCallObject+0x70 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\call.cxx @ 803]
03 01e4f8f4 77b29851 00000000 6d5f10e0 01e4f924 ole32!CRpcChannelBuffer::ClientGetBuffer+0x17f (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 2117]
04 01e4f904 77aa70c4 000b161c 01e4f9ec 6d5f10e0 ole32!CRpcChannelBuffer::GetBuffer+0x17 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 2005]
05 01e4f924 77aa6f60 000b161c 01e4f9ec 6d5f10e0 ole32!CAptRpcChnl::GetBuffer+0x11d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 552]
06 01e4f984 77d94b21 000b161c 00000000 6d5f10e0 ole32!CCtxComChnl::GetBuffer+0x1a2 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 529]
07 01e4f99c 77d93d5e 000c3104 01e4fa38 05030117 RPCRT4!NdrProxyGetBuffer+0x3b (FPO: [2,0,2]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\proxy.c @ 1898]
08 01e4fbe4 77d94c61 6d5f11b0 6d5f1230 01e4fc18 RPCRT4!NdrClientCall2+0x3c6 (FPO: [Non-Fpo]) (CONV: cdecl) [D:\nt\private\rpc\ndr20\cltcall.c @ 1281]
09 01e4fc00 77d493aa 00000018 00000004 01e4fc4c RPCRT4!ObjectStublessClient+0x76 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\i386\stblsclt.c @ 454]
0a 01e4fc10 65d87fad 000c3104 00da1ea0 00000001 RPCRT4!ObjectStubless+0xf [..\i386\stubless.asm @ 154]
0b 01e4fc4c 749f80a8 00da1e90 00000004 01e4fc78 wam!ServerSupportFunction+0x2f3 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 1151]
0c 01e4fc7c 749fcfdf 00000000 00000000 01b51e90 asp!CResponse::FinalFlush+0x226 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\response.cpp @ 4063]
0d 01e4fcc0 749fcc37 00000000 000be1d8 01e4fd58 asp!CHitObj::ViperAsyncCallback+0x492 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\hitobj.cpp @ 2504]
0e 01e4fcd8 787c214a 00000000 77acd455 01b51e90 asp!CViperAsyncRequest::OnCall+0x3f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\viperint.cpp @ 194]
0f 01e4fce0 77acd455 01b51e90 77aa71a0 00000000 COMSVCS!STAActivityWorkHelper+0xa (FPO: [1,0,0]) (CONV: stdcall) [.\stathred.cpp @ 1321]
10 01e4fd24 77acd318 000751d8 00085850 787c2140 ole32!EnterForCallback+0x6a (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1758]
11 01e4fe50 77acd1dd 00085850 787c2140 01b51e90 ole32!SwitchForCallback+0x12b (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1643]
12 01e4fe78 77aac61a 00085850 787c2140 01b51e90 ole32!PerformCallback+0x50 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1558]
13 01e4fed4 77ad1185 000757c0 787c2140 01b51e90 ole32!CObjectContext::InternalContextCallback+0xf5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\context.cxx @ 3942]
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 scan the process heap and look for unreadable heap entry:
0:020> !heap 00070000 -m
Index Address Name Debugging options enabled
1: 00070000
Segment at 00070000 to 00170000 (00057000 bytes committed)
Flags: 00000002
ForceFlags: 00000000
Granularity: 8 bytes S
egment Reserve: 00100000
Segment Commit: 00002000
DeCommit Block Thres:00000200
DeCommit Total Thres:00002000
Total Free Size: 000000d6
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: 00000000 00000000 00000000 00000000
FreeList[ 00 ] at 00070178: 000c6958 . 000c6958
Unabled to read HEAP_ENTRY structure at 61616159
Segment00 at 00070640:
Flags: 00000000
Base: 00070000
First Entry: 00070680
Last Entry: 00170000
Total Pages: 00000100
Total UnCommit: 000000a9
Largest UnCommit:000a9000
UnCommitted Ranges: (1) 000c7000: 000a9000
unable to read heap entry at 000f7458
So this shows that heap is corrupt(because the Free list is corrupt)
What this tells us is that the heap at " 00070000 " is corrupted. The thread displayed above was somehow trying to manipulate the 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
The stack backtrace of the thread that AV'd would look like:
0:020> kvn
# ChildEBP RetAddr Args to Child
00 06a2f0f4 06e2298c 00000001 63683d73 31765f31 badcom!doHC1+0x24 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 279]
01 06a2f124 779d7b9d 06e11ff0 06dffef0 00000000 badcom!CScenarios::processSc+0x142 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 138]
02 06a2f140 77a22c42 06e11ff0 00000024 00000004 OLEAUT32!tPushValJmpTab+0xf5 [D:\OA\src\dispatch\WIN32\i386\invoke.asm @ 340]
03 06a2f1d0 06e225d0 06cdffac 06e11ff0 00000003 OLEAUT32!CTypeInfo2::Invoke+0x244 (CONV: stdcall) [D:\OA\src\typelib2\tiinvoke.cpp @ 629]
04 06a2f1f8 6b61dff3 06e11ff0 00000003 6b659388 badcom!ATL::IDispatchImpl::Invoke+0x41 (CONV: stdcall) [c:\nt\public\sdk\inc\atl30\atlcom.h @ 4093]
05 06a2f274 6b62171a 005cbfe0 06e11ff0 00000003 vbscript!CatchIDispatchInvoke+0x9e (CONV: stdcall) [E:\vbscript\src\core\callout.h @ 111]
06 06a2f384 6b604038 0009001f 0000023f 06de7f80 vbscript!InvokeDispatch+0x330 (FPO: [EBP 0x6b659388] [7,60,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 1211]
07 06a2f3c8 6b603156 06de7f80 06e11ff0 00000003 vbscript!GetDispatchDispID+0x1cc (FPO: [EBP 0x06a2f954] [4,3,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 972]
08 06a2f954 77fcbecd 06d7bfe0 00000000 00000030 vbscript!CScriptRuntime::Run+0x3b8d (CONV: thiscall) [E:\vbscript\src\vbscript\interp.cpp @ 3347]
09 06a20000 00000000 00000000 00000000 00000000 ntdll!RtlAllocateHeap+0x8be (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 2161]
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 "doHC1" as well as the line of code in "badfil.cpp".
Verify the extension's symbols are lined up.
0:021> lmv mbadcom
start end module name
02030000 02038000 badcom (pdb symbols) c:\iisdebuglabs\symbols\pub\badcom.pdb
Loaded symbol image file: badcom.dll
Image path: c:\IISDEB~1\www\app\badcom.dll
Timestamp: Tue Jun 10 22:45:51 2003 (3EE697DF) Checksum: 0000F229
File version: 1.0.0.1
Product version: 1.0.0.1
File flags: 0 (Mask 3F)
File OS: 4 Unknown Win32
File type: 2.0 Dll
File date: 00000000.00000000
CompanyName:
ProductName: badCOM
Module InternalName: badCOM
OriginalFilename: badCOM.DLL
ProductVersion: 1, 0, 0, 1
FileVersion: 1, 0, 0, 1
FileDescription: badCOM Module
LegalCopyright: Copyright 2003
Line 279 looks like
DWORD doHC1(int Variation) //Buffer Overwrite
{
if (1 == Variation) // Straight forward to corrupt the heap
{
char *ptr, *tmp;
int i;
ptr = (char*)GlobalAlloc(GMEM_FIXED, 16);
tmp = ptr;
for (i = 0; i < 32; ++i)
*(tmp++) = 'a';
free(ptr); // line 279
}
}