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 freeing a block of memory already freed (Double Free).
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 Extension
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:023> kvn
# ChildEBP RetAddr Args to Child
00 01ecfab0 6b60e9a4 00081220 6b630462 0221a658 vbscript!CSession::Close+0x72 (FPO: [EBP 0x01ecfab4] [0,0,4]) (CONV: thiscall) [E:\vbscript\src\core\wrapper.cpp @ 227]
01 01ecfab8 6b630462 0221a658 6b604b64 022113a4 vbscript!CSession::~CSession+0x8 (FPO: [0,0,1]) (CONV: thiscall) [E:\vbscript\src\core\wrapper.cpp @ 150]
02 01ecfac0 6b604b64 022113a4 6b6010c3 02207468 vbscript!CScriptEntryPoint::~CScriptEntryPoint+0x32 (FPO: [0,0,1]) (CONV: thiscall) [E:\vbscript\src\vbscript\entrypnt.cpp @ 23]
03 01ecfac8 6b6010c3 02207468 022113a4 77e873b2 vbscript!CScriptEntryPoint::Release+0x15 (FPO: [0,0,1]) (CONV: thiscall) [E:\vbscript\src\vbscript\entrypnt.h @ 38]
04 01ecfad8 6b60e598 02212db8 02207468 6b60e55c vbscript!VAR::Clear+0xf6 (FPO: [0,0,3]) (CONV: thiscall) [E:\vbscript\src\vbscript\var.cpp @ 74]
05 01ecfae4 6b60e55c 02212db8 00000000 6b60e50f vbscript!NameList::~NameList+0x12 (FPO: [0,0,2]) (CONV: thiscall) [E:\vbscript\src\core\nametbl.cpp @ 2813]
06 01ecfaf0 6b60e50f 02212db8 02212dbc 6b60e4df vbscript!NameTbl::Close+0x12 (FPO: [0,0,2]) (CONV: thiscall) [E:\vbscript\src\core\nametbl.cpp @ 104]
07 01ecfafc 6b60e4df 00000000 02201220 74a33180 vbscript!ModuleBinder::Close+0x3e (FPO: [0,0,2]) (CONV: thiscall) [E:\vbscript\src\core\nametbl.cpp @ 2552]
08 01ecfb0c 6b60e46e 02212db8 77e878b2 02213330 vbscript!GlobalBinder::Release+0x25 (FPO: [1,0,3]) (CONV: stdcall) [E:\vbscript\src\core\nametbl.cpp @ 3794]
09 01ecfb34 6b610cf7 02213330 00000001 012d1fd0 vbscript!CSession::Close+0x75 (FPO: [EBP 0x02213330] [0,0,4]) (CONV: thiscall) [E:\vbscript\src\core\wrapper.cpp @ 228]
0a 01ecfb44 749f62e0 02213330 00000000 749f62b2 vbscript!COleScript::SetScriptState+0x16e (FPO: [2,0,1]) (CONV: stdcall) [E:\vbscript\src\core\colescpt.cpp @ 931]
0b 01ecfb50 749f62b2 74a332c0 012d1fd0 74a33180 asp!CActiveScriptEngine::ResetScript+0xf (FPO: [0,0,0]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\ScrptMgr.h @ 308]
0c 01ecfbb8 749f610c 00000000 00000000 01ecfc4c asp!CActiveScriptEngine::ResetToUninitialized+0x190 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\scrptmgr.cpp @ 629]
0d 01ecfbd8 749f606f 01ecfc60 01311e90 00000000 asp!CScriptManager::ReturnEngineToCache+0x74 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\scrptmgr.cpp @ 2688]
0e 01ecfbf4 749f597d 01ecfc4c 01311e90 00000000 asp!DeAllocAndFreeEngines+0x35 (FPO: [3,0,3]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 1299]
0f 01ecfc28 749f54c8 01a121c0 01592220 01ecfc94 asp!ExecuteRequest+0x278 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 398]
10 01ecfc70 749fcf9b 01a121c0 01592220 01ecfc94 asp!Execute+0x17e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 190]
11 01ecfcc0 749fcc37 00000000 000bf608 01ecfd58 asp!CHitObj::ViperAsyncCallback+0x452 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\hitobj.cpp @ 2487]
12 01ecfcd8 787c214a 00000000 77acd455 01b51f80 asp!CViperAsyncRequest::OnCall+0x3f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\viperint.cpp @ 194]
13 01ecfce0 77acd455 01b51f80 77aa71a0 00000000 COMSVCS!STAActivityWorkHelper+0xa (FPO: [1,0,0]) (CONV: stdcall) [.\stathred.cpp @ 1321]
The faulting stack in a heap corruption issue will involve heap operations. In this example we don't see direct calls to NTDLL heap
management fuctions, but it does not mean that this faulting stack is because of heap corruption.
Let scan the process heap and look for unreadable heap entry
0:023> ! heap 0 -m
Index Address Name Debugging options enabled
.......
3: 00400000
Segment at 00400000 to 00410000 (00010000 bytes committed)
Segment at 02200000 to 02300000 (00072000 bytes committed)
Flags: 00001002
ForceFlags: 00000000
Granularity: 8 bytes
Segment Reserve: 00200000
Segment Commit: 00002000
DeCommit Block Thres:00000200
DeCommit Total Thres:00002000
Total Free Size: 00000957
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: 004005B8
UCRSegment - 02480000: 00001000 . 00010000
FreeList Usage: 00000004 00010000 00000000 00000000
FreeList[ 00 ] at 00400178: 02226010 . 022216e8
FreeList[ 02 ] at 00400188: 02202528 . 0040b760
Unabled to read HEAP_ENTRY structure at 00000000
FreeList[ 30 ] at 004002f8: 0221e878 . 0221e878
Segment00 at 00400640:
Flags: 00000000
Base: 00400000
First Entry: 00400680
Last Entry: 00410000
Total Pages: 00000010
Total UnCommit: 00000000
Largest UnCommit:00000000
UnCommitted Ranges: (0)
.......
So what this tells us is that the heap at " 00040000 " is corrupted. The thread displayed above was trying to coalesce 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
0:022> g
Page heap: block @ 02F10FF0 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
(8e0.73c): Break instruction exception - code 80000003 (first chance)
eax=00000000 ebx=00000027 ecx=77fb3d26 edx=06aaec9a esi=02f10fd0 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 02f10ff0 77fb4248 02f10ff0 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]
01 06aaee8c 77fb4248 02f10ff0 00000027 00511000 ntdll!RtlpDphReportCorruptedBlock+0x8c (FPO: [2,0,1]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4142]
02 06aaeeb4 77fb2f3f 00511000 01001002 02f10ff0 ntdll!RtlpDphNormalHeapFree+0x46 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4575]
03 06aaeed8 77fb5887 00510000 01001002 02f10ff0 ntdll!RtlpDebugPageHeapFree+0xa6 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 2862]
04 06aaef50 77fa7111 00510000 01001002 02f10ff0 ntdll!RtlDebugFreeHeap+0x203 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 915]
05 06aaeff0 77fcbeda 00510000 01001002 02f10ff0 ntdll!RtlFreeHeapSlowly+0x4d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3466]
06 06aaf094 7800115c 00510000 00000000 02f10ff0 ntdll!RtlFreeHeap+0x53 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3138]
07 06aaf0dc 06e62d71 02f10ff0 06d62ff0 78002041 MSVCRT!free+0xda (CONV: cdecl) [free.c @ 101]
08 06aaf0f4 06e629a9 00000001 63683d73 31765f32 badcom!doHC2+0x2f (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 314]
09 06aaf124 779d7b9d 00567ff0 00573ef0 00000000 badcom!CScenarios::processSc+0x15f (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 142]
0a 06aaf140 77a22c42 00567ff0 00000024 00000004 OLEAUT32!tPushValJmpTab+0xf5 [D:\OA\src\dispatch\WIN32\i386\invoke.asm @ 340]
0b 06aaf1d0 06e625d0 06852fac 00567ff0 00000003 OLEAUT32!CTypeInfo2::Invoke+0x244 (CONV: stdcall) [D:\OA\src\typelib2\tiinvoke.cpp @ 629]
0c 06aaf1f8 6b61dff3 00567ff0 00000003 6b659388 badcom!ATL::IDispatchImpl::Invoke+0x41 (CONV: stdcall) [c:\nt\public\sdk\inc\atl30\atlcom.h @ 4093]
0d 06aaf274 6b62171a 06d60fe0 00567ff0 00000003 vbscript!CatchIDispatchInvoke+0x9e (CONV: stdcall) [E:\vbscript\src\core\callout.h @ 111]
0e 06aaf384 6b604038 0009001f 0000023f 005a3f80 vbscript!InvokeDispatch+0x330 (FPO: [EBP 0x6b659388] [7,60,4]) (CONV: stdcall) [E:\vbscript\src\vbscript\var.cpp @ 1211]
0f 06aaf3c8 6b603156 005a3f80 00567ff0 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 005a3f80 6b606ef1 06dbffe0 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 "badComl.dll".
Notice also the function call "doHC2" 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 314 looks like
DWORD doHC2(int Variation)//Double Free
{
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);
free(tmp);
free (ptr); //line 314
return (DWORD) ptr;
}
...
}