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
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:028> kvn
# ChildEBP RetAddr Args to Child
00 02e1f0a0 780010a8 00400000 00000000 00000010 ntdll!RtlAllocateHeap+0x293 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 1703]
01 02e1f0e0 78001045 0000000a 780026df 0000000a MSVCRT!_heap_alloc+0xed (CONV: cdecl) [malloc.c @ 207]
02 02e1f0e8 780026df 0000000a 00000001 020328c2 MSVCRT!_nh_malloc+0x10 (FPO: [2,0,0]) (CONV: cdecl) [malloc.c @ 113]
03 02e1f0f4 020328c2 0000000a 63683d73 31765f33 MSVCRT!operator new+0xb (FPO: [1,0,0]) (CONV: cdecl) [new.cpp @ 24]
04 02e1f124 779d7b9d 039791b0 035bed78 00000000 badcom!CScenarios::processSc+0x78 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 199]
05 02e1f140 77a22c42 039791b0 00000024 00000004 OLEAUT32!tPushValJmpTab+0xf5 [D:\OA\src\dispatch\WIN32\i386\invoke.asm @ 340]
06 02e1f1d0 020325d0 000c0ad4 039791b0 00000003 OLEAUT32!CTypeInfo2::Invoke+0x244 (CONV: stdcall) [D:\OA\src\typelib2\tiinvoke.cpp @ 629]
07 02e1f1f8 6b61dff3 039791b0 00000003 6b659388 badcom!ATL::IDispatchImpl::Invoke+0x41 (CONV: stdcall) [c:\nt\public\sdk\inc\atl30\atlcom.h @ 4093]
08 02e1f274 6b62171a 02ae2838 039791b0 00000003 vbscript!CatchIDispatchInvoke+0x9e (CONV: stdcall) [E:\vbscript\src\core\callout.h @ 111]
09 02e1f384 6b604038 0009001f 0000023f 02abb820 vbscript!InvokeDispatch+0x330 (FPO: [EBP 0x6b659388] [7,60,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 1211]
0a 02e1f3c8 6b603156 02abb820 039791b0 00000003 vbscript!GetDispatchDispID+0x1cc (FPO: [EBP 0x02e1f954] [4,3,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 972]
0b 02e1f998 6b601ef7 00000009 02e1fa8c 00000000 vbscript!CScriptRuntime::Run+0x3b8d (CONV: thiscall) [E:\vbscript\src\vbscript\interp.cpp @ 3347]
0c 02e1f9c8 6b601c5c 00000000 00000000 00000000 vbscript!CScriptRuntime::Init+0x2e1 (FPO: [EBP 0x6b658a10] [6,0,4]) (CONV: thiscall) [E:\vbscript\src\vbscript\interp.cpp @ 904]
0d 02e1fb08 6b606fc5 02abb820 6b606ef1 00409640 vbscript!CScriptEntryPoint::Call+0x76 (FPO: [5,58,2]) (CONV: thiscall) [E:\vbscript\src\vbscript\entrypnt.cpp @ 40]
0e 02e1fb54 749f2e70 01915310 6b610c9a 00000000 vbscript!CSession::GetGlobalEntryPoint+0x25 (FPO: [3,0,0]) (CONV: thiscall) [E:\vbscript\src\core\wrapper.cpp @ 485]
0f 02e1fb6c 749f3af0 019154e0 019154e0 012d24d0 asp!CResponse::Release+0x18 (FPO: [1,0,1]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\response.cpp @ 1716]
10 02e1fb9c 74a22f5b 00000000 74a32c70 77e873a1 asp!CActiveScriptEngine::TryCall+0x15 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\scrptmgr.cpp @ 2040]
11 02e1fbe0 749f3a72 00000000 02e1fc28 749f593d asp!CActiveScriptEngine::Call+0x10c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\scrptmgr.cpp @ 2005]
12 02e1fbec 749f593d 02e1fc4c 00000000 00000000 asp!CallScriptFunctionOfEngine+0x48 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 1394]
13 02e1fc28 749f54c8 01a11fa0 015926e0 02e1fc94 asp!ExecuteRequest+0x19e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 326]
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 " 00400000 " looks like:
0:015> !heap 00400000 -m
Index Address Name Debugging options enabled
1: 00070000
2: 00170000
3: 00400000
Segment at 00400000 to 00410000 (00010000 bytes committed)
Segment at 020D0000 to 021D0000 (00100000 bytes committed)
Segment at 02A20000 to 02C20000 (00200000 bytes committed)
Segment at 02FE0000 to 033E0000 (00400000 bytes committed)
Segment at 035A0000 to 03DA0000 (0040d000 bytes committed)
Flags: 00001002
ForceFlags: 00000000
Granularity: 8 bytes
Segment Reserve: 01000000
Segment Commit: 00002000
DeCommit Block Thres:00000200
DeCommit Total Thres:00002000
Total Free Size: 00000a4c
Max. Allocation Size:7ffdefff
Lock Variable at: 00400608
Next TagIndex: 0000
Maximum TagIndex: 0000
Tag Entries: 00000000
PsuedoTag Entries: 00000000
Virtual Alloc List: 00400050 UCR
FreeList: 00400598
FreeList Usage: 2a820114 00004400 80021000 01000020
FreeList[ 00 ] at 00400178: 039ab438 . 00400178
FreeList[ 02 ] at 00400188: 02ae5978 . 00409700
FreeList[ 04 ] at 00400198: 000d6e00 . 02170690
Unabled to read HEAP_ENTRY structure at 0000316e
FreeList[ 08 ] at 004001b8: 0215f170 . 0215f170
FreeList[ 11 ] at 00400200: 02ae21d8 . 03083868
So what this tells us is that the heap at " 00400000 " is corrupted. The thread displayed above was trying to allocate some memory (10 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".
- 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:022> g
Page heap: block @ 06FDEFF0 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
(81c.b24): Break instruction exception - code 80000003 (first chance)
eax=00000000 ebx=00000027 ecx=77fb3d26 edx=06aaec9a esi=06fdefd0 edi= 00511000
eip=77f97704 esp=06aaee88 ebp=06aaeeb4 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:022> kvn
# ChildEBP RetAddr Args to Child
00 06aaee84 77fb3de6 06fdeff0 77fb4248 06fdeff0 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]
01 06aaee8c 77fb4248 06fdeff0 00000027 00511000 ntdll!RtlpDphReportCorruptedBlock+0x8c (FPO: [2,0,1]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4142]
02 06aaeeb4 77fb2f3f 00511000 01001002 06fdeff0 ntdll!RtlpDphNormalHeapFree+0x46 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4575]
03 06aaeed8 77fb5887 00510000 01001002 06fdeff0 ntdll!RtlpDebugPageHeapFree+0xa6 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 2862]
04 06aaef50 77fa7111 00510000 01001002 06fdeff0 ntdll!RtlDebugFreeHeap+0x203 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 915]
05 06aaeff0 77fcbeda 00510000 01001002 06fdeff0 ntdll!RtlFreeHeapSlowly+0x4d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3466]
06 06aaf094 7800115c 00510000 00000000 06fdeff0 ntdll!RtlFreeHeap+0x53 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3138]
07 06aaf0dc 06e62de4 06fdeff0 06e25ff0 78002041 MSVCRT!free+0xda (CONV: cdecl) [free.c @ 101]
08 06aaf0f4 06e629c6 00000001 63683d73 31765f33 badcom!doHC3+0x2a (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 353]
09 06aaf124 779d7b9d 06e1fff0 06e09ef0 00000000 badcom!CScenarios::processSc+0x17c (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 146]
0a 06aaf140 77a22c42 06e1fff0 00000024 00000004 OLEAUT32!tPushValJmpTab+0xf5 [D:\OA\src\dispatch\WIN32\i386\invoke.asm @ 340] 0b
06aaf1d0 06e625d0 06d1efac 06e1fff0 00000003 OLEAUT32!CTypeInfo2::Invoke+0x244 (CONV: stdcall) [D:\OA\src\typelib2\tiinvoke.cpp @ 629]
0c 06aaf1f8 6b61dff3 06e1fff0 00000003 6b659388 badcom!ATL::IDispatchImpl::Invoke+0x41 (CONV: stdcall) [c:\nt\public\sdk\inc\atl30\atlcom.h @ 4093]
0d 06aaf274 6b62171a 06d60fe0 06e1fff0 00000003 vbscript!CatchIDispatchInvoke+0x9e (CONV: stdcall) [E:\vbscript\src\core\callout.h @ 111]
0e 06aaf384 6b604038 0009001f 0000023f 06deff80 vbscript!InvokeDispatch+0x330 (FPO: [EBP 0x6b659388] [7,60,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 1211]
0f 06aaf3c8 6b603156 06deff80 06e1fff0 00000003 vbscript!GetDispatchDispID+0x1cc (FPO: [EBP 0x06aaf954] [4,3,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 972]
10 06aaf970 78001045 00000000 06da7d8c 00000009 vbscript!CScriptRuntime::Run+0x3b8d (CONV: thiscall) [E:\vbscript\src\vbscript\interp.cpp @ 3347]
11 06aaf998 6b601ef7 00000009 06aafa8c 00000000 MSVCRT!_nh_malloc+0x10 (FPO: [2,0,0]) (CONV: cdecl) [malloc.c @ 113]
12 06aaf9c8 6b601c5c 00000000 00000000 00000000 vbscript!CScriptRuntime::Init+0x2e1 (FPO: [EBP 0x6b658a10] [6,0,4]) (CONV: thiscall) [E:\vbscript\src\vbscript\interp.cpp @ 904]
13 06aafb08 6b606fc5 06deff80 6b606ef1 06e17fe0 vbscript!CScriptEntryPoint::Call+0x76 (FPO: [5,58,2]) (CONV: thiscall) [E:\vbscript\src\vbscript\entrypnt.cpp @ 40]
So basically, this is the thread that corrupted the heap, It's shows now that the culprit is "badCom.dll".
Notice also the function call "doHC3" as well as the line of code in "badCom.cpp"
Verify the filter'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 353 looks like
DWORD doHC3(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 353
*ptr = 'x';
return (DWORD) ptr;
}
...
}