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 Filter
a) Using the GUI:
- Click "File"
- Click "Symbol File Path"
- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pri a nd 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:045> kvn50
# ChildEBP RetAddr Args to Child
00 0315fc00 68511248 00070000 0000000c 00000014 ntdll!RtlAllocateHeap+0x125 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 1574]
01 0315fc10 685113ed 00000014 00000000 00000002 RWNH!CHandleInfo::operator new+0x13 (FPO: [1,0,0]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\staxcore\rwnh\src\rwintrnl.cpp @ 68]
02 0315fc24 68511570 685114b1 00000002 685114fa RWNH!CHandleInfo::AllocHandleInfo+0x41 (FPO: [0,0,3]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\staxcore\rwnh\src\rwintrnl.cpp @ 248]
03 0315fc28 685114b1 00000002 685114fa 68510000 RWNH!CWaitingThread::ThreadEnter+0x5 (FPO: [0,0,0]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\staxcore\rwnh\src\rwintrnl.cpp @ 388]
04 0315fc30 685114fa 68510000 00000002 00000000 RWNH!DllEntryPoint+0x1f (FPO: [3,0,1]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\staxcore\rwnh\src\rwintrnl.cpp @ 323]
05 0315fc40 68511e86 68510000 00000002 00000000 RWNH!DllMain+0x11 (FPO: [3,0,0]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\staxcore\rwnh\src\rwintrnl.cpp @ 347]
06 0315fc60 77f85058 68510000 00000002 00000000 RWNH!_DllMainCRTStartup+0x66 (FPO: [EBP 0x0315fc80] [3,0,4]) (CONV: stdcall) [crtdll.c @ 276]
07 0315fc80 77f86831 68511e20 68510000 00000002 ntdll!LdrpCallInitRoutine+0x14 [..\i386\ldrthunk.asm @ 140]
08 0315fd1c 77fa02e3 0315fd30 77f80000 00000000 ntdll!LdrpInitializeThread+0x7f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\dll\ldrinit.c @ 1774]
09 00000000 00000000 00000000 00000000 00000000 ntdll!KiUserApcDispatcher+0x7 [..\i386\userdisp.asm @ 103]
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:045> !heap 00070000 -m
Index Address Name Debugging options enabled
1: 00070000
Segment at 00070000 to 00170000 (00100000 bytes committed)
Segment at 02DC0000 to 02EC0000 (00009000 bytes committed)
Flags: 00000002
ForceFlags: 00000000
Granularity: 8 bytes
Segment Reserve: 00200000
Segment Commit: 00002000
DeCommit Block Thres:00000200
DeCommit Total Thres:00002000
Total Free Size: 000002af
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: 00000004 00000000 00000000 00000000
FreeList[ 00 ] at 00070178: 02dc7ab0 . 02dc7ab0
FreeList[ 02 ] at 00070188: 00165880 . 00160628
FreeList[ 03 ] at 00070190: 00080190 . 00070190
FreeList[ 04 ] at 00070198: 00000000 . 77ab0728
Unabled to read HEAP_ENTRY structure at 56ec8b4d
FreeList[ 05 ] at 000701a0: 00000000 . 00000000
Unabled to read HEAP_ENTRY structure at fffffff8
FreeList[ 06 ] at 000701a8: 00000003 . 00000003
Unabled to read HEAP_ENTRY structure at fffffffb
Segment00 at 00070640:
Flags: 00000000
Base: 00070000
First Entry: 00070680
Last Entry: 00170000
Total Pages: 00000100
Total UnCommit: 00000000
Largest UnCommit:00000000
UnCommitted Ranges: (0)
Segment01 at 02dc0000:
Flags: 00000000
Base: 02dc0000
First Entry: 02dc0040
Last Entry: 02ec0000
Total Pages: 00000100
Total UnCommit: 000000f7
Largest UnCommit:000f7000
UnCommitted Ranges: (1)
02dc9000: 000f7000
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 "inetinfo.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:007> g
Page heap: block @ 0677CFF0 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
(13c.254): Break instruction exception - code 80000003 (first chance)
eax=00000000 ebx=00000027 ecx=77fb3d26 edx=049df186 esi=0677cfd0 edi=00341000
eip=77f97704 esp=049df374 ebp=049df3a0 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:007> kpn
# ChildEBP RetAddr
00 049df370 77fb3de6 ntdll!DbgBreakPoint [..\i386\debug2.asm @ 42]
01 049df378 77fb4248 ntdll!RtlpDphReportCorruptedBlock(void* Block = 0677cff0 , unsigned long Reason = 0x27)+0x8c [D:\nt\private\ntos\rtl\heappage.c @ 4142]
02 049df3a0 77fb2f3f ntdll!RtlpDphNormalHeapFree(_DPH_HEAP_ROOT* Heap = 00341000 , unsigned long Flags = 0x1001002, void* Block = 0677cff0 )+0x46 [D:\nt\private\ntos\rtl\heappage.c @ 4575]
03 049df3c4 77fb5887 ntdll!RtlpDebugPageHeapFree(void* HeapHandle = 00340000 , unsigned long Flags = 0x1001002, void* Address = 0677cff0 )+0xa6 [D:\nt\private\ntos\rtl\heappage.c @ 2862]
04 049df43c 77fa7111 ntdll!RtlDebugFreeHeap(void* HeapHandle = 00340000 , unsigned long Flags = 0x1001002, void* BaseAddress = 0677cff0 )+0x203 [D:\nt\private\ntos\rtl\heapdbg.c @ 915]
05 049df4dc 77fcbeda ntdll!RtlFreeHeapSlowly(void* HeapHandle = 00340000 , unsigned long Flags = 0x1001002, void* BaseAddress = 0677cff0 )+0x4d [D:\nt\private\ntos\rtl\heap.c @ 3466]
06 049df580 7800115c ntdll!RtlFreeHeap(void* HeapHandle = 00340000 , unsigned long Flags = 0, void* BaseAddress = 0677cff0 )+0x53 [D:\nt\private\ntos\rtl\heap.c @ 3138]
07 049df5c8 05a01b7c MSVCRT!free(void* pBlock = 0677cff0 )+0xda [free.c @ 101]
08 049dfbf4 65f0354f badfil!doHC2(int Variation = 0x65f0354f)+0x2f [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\filDBG\badfil.cpp @ 608]
09 049dfc1c 65f034bc w3svc!FILTER_LIST::NotifyFilters(HTTP_FILTER* pFilter = 000003e9 , unsigned long NotificationType = 0x4000, _HTTP_FILTER_CONTEXT* phfc = 00000002 , void* NotificationData = 049dfc3c , int* pfFinished = 049dfedc , int fNotifyAll = 0)+0x9d [D:\nt\private\inet\iis\svcs\w3\server\httpfilt.cxx @ 968]
0a 049dfc50 65f02cad w3svc!HTTP_FILTER::NotifyPreProcHeaderFilters(int* pfFinished = 049dfedc )+0x4c [.\filter.hxx @ 473]
0b 049dfca8 65f02ac1 w3svc!HTTP_REQUEST::Parse(char* pchRequest = 0612ee60 "GET /IISDebugLabs/site/fil_hc2.htm?sf=hc2_v1 HTTP/1.1???", unsigned long cbData = 0x19b, unsigned long* pcbExtraData = 049dfcd4 , int* pfHandled = 049dfce8 , int* pfFinished = 049dfedc )+0x380 [D:\nt\private\inet\iis\svcs\w3\server\httpreq.cxx @ 1497]
0c 049dfcec 65f029b5 w3svc!HTTP_REQ_BASE::OnCompleteRequest(char* pchRequest = 0612ee60 "GET /IISDebugLabs/site/fil_hc2.htm?sf=hc2_v1 HTTP/1.1???", unsigned long cbData = 0x19b, int* pfFinished = 049dfedc , int* pfContinueProcessingRequest = 049dfeb8 )+0x36 [D:\nt\private\inet\iis\svcs\w3\server\httpio.cxx @ 1289]
0d 049dfd2c 65f028f8 w3svc!HTTP_REQ_BASE::UnWrapRequest(int* pfCompleteRequest = 049dfea4 , int* pfFinished = 049dfedc , int* pfContinueProcessingRequest = 049dfeb8 )+0x1f4 [D:\nt\private\inet\iis\svcs\w3\server\httpio.cxx @ 634]
0e 049dfd4c 65f027b3 w3svc!HTTP_REQ_BASE::OnFillClientReq(int* pfCompleteRequest = 049dfea4 , int* pfFinished = 049dfedc , int* pfContinueProcessingRequest = 049dfeb8 )+0x7a [D:\nt\private\inet\iis\svcs\w3\server\httpio.cxx @ 253]
0f 049dfec4 65f020d7 w3svc!HTTP_REQUEST::DoWork(int* pfFinished = 049dfedc )+0x99 [D:\nt\private\inet\iis\svcs\w3\server\httpreq.cxx @ 639]
10 049dfee4 65f01b47 w3svc!CLIENT_CONN::DoWork(unsigned long BytesWritten = 0, unsigned long CompletionStatus = 0, int fIOCompletion = 0)+0x1aa [D:\nt\private\inet\iis\svcs\w3\server\conn.cxx @ 781]
11 049dff08 65f01aa5 w3svc!CreateClient(_CLIENT_CONN_PARAMS* ClientParam = 049dff20 )+0x7b [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 310]
12 049dff4c 6d701443 w3svc!W3OnConnectEx(void* patqContext = 04808eb0 , unsigned long cbWritten = 0x19b, unsigned long err = 0x61a87c0, _OVERLAPPED* lpo = 05fe6f90 )+0x118 [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 215]
13 049dff80 6d702203 ISATQ!AtqpProcessContext(ATQ_CONTEXT* pAtqContext = 00000000 , unsigned long cbWritten = 0x19b, _OVERLAPPED* lpo = 04808eb4 , int fRet = 1)+0x23a [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 883]
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:008> lmv mbadfil
start end module name
015a0000 015a6000 badfil (pdb symbols) C:\IISDebugLabs\Symbols\Pri
Loaded symbol image file: badfil.dll
Image path: C:\IISDebugLabs\www\App\badfil.dll
Timestamp: Wed May 28 01:11:01 2003 (3ED444E5)
Checksum: 0000D9A0
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: badFIL.dll
OriginalFilename: badFIL.dll
ProductVersion: 5.00.0984
FileVersion: 5.00.0984
FileDescription: badFIL Filter DLL
LegalCopyright: Copyright (C) Microsoft Corp. 1981-1999
Line 608 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 608
return (DWORD) ptr;
}
...
}