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:009> kvn50
# ChildEBP RetAddr Args to Child
00 0132fb88 74fdc09f 00070000 00000000 00000020 ntdll!RtlAllocateHeap+0x361 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 2063]
01 0132fba8 74fdbf3d 000febb0 0132fc08 74fdac55 msafd!SockThreadInitialize+0x33 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\wsp\msafd\sockproc.c @ 2650]
02 0132fbb4 74fdac55 0132fbdc 0000700b 000febb0 msafd!SockEnterApiSlow+0x37 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\wsp\msafd\sockproc.c @ 1662]
03 0132fc08 556119f4 000007cc 0000ffff 0000700b msafd!WSPSetSockOpt+0x5a (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\wsp\msafd\sockopt.c @ 2573]
04 0132fc3c 750319b7 000007cc 0000ffff 0000700b wspwsp!WSPSetSockOpt+0x2e4 (FPO: [Non-Fpo]) (CONV: stdcall) [s:\nt\private\comet\msp\haifa\rws\client\ws2\socket.cpp @ 874]
05 0132fca8 6d701876 000007cc 0000ffff 0000700b WS2_32!setsockopt+0xcf (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\dll\winsock2\sockctrl.cpp @ 621]
06 0132fcd0 65f06720 00000000 00000001 77e87684 ISATQ!AtqCloseSocket+0x83 (FPO: [EBP 0x0132ff08] [2,0,4]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\infocomm\atq\atqmain.cxx @ 1436]
07 0132ff08 65f1e2b5 00000000 00000000 00000000 w3svc!CLIENT_CONN::Disconnect+0x8c6 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\w3\server\conn.cxx @ 1473]
08 0132ff38 65f093eb 00001087 00000000 00000001 w3svc!CLIENT_CONN::DoWork+0x27c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\w3\server\conn.cxx @ 884]
09 0132ff4c 6d701394 00eb55c8 00001087 00000000 w3svc!W3Completion+0x43 (FPO: [4,0,1]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\w3\server\conn.cxx @ 1857]
0a 0132ff80 6d702203 00000000 00001087 012742dc ISATQ!AtqpProcessContext+0x262 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 912]
0b 0132ffb4 77e887dd abcdef01 00000000 77fcb650 ISATQ!AtqPoolThread+0x1a8 (FPO: [EBP 0x0132ffec] [1,4,4]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 1127]
0c 0132ffec 00000000 6d7091f7 abcdef01 00000000 KERNEL32!BaseThreadStart+0x52 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 460]
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:009> !heap 00070000 -m
Index Address Name Debugging options enabled
1: 00070000
Segment at 00070000 to 00170000 (000e2000 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: 00000587
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: 0014f3d0 . 0014f3d0
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: 0000001e
Largest UnCommit:0001e000
UnCommitted Ranges: (1) 00152000: 0001e000
unable to read heap entry at 00152dd8
So what this tells us is that the heap at " 00070000 " is corrupted. The thread displayed above was trying to allocate some memory (20 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
The stack backtrace of the thread that AV'd would look like:
0:008> kpn
# ChildEBP RetAddr
00 049dfbf4 65f0354f badfil!doHC1(int Variation = 0x65f0354f)+0x23 [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\filDBG\badfil.cpp @ 571]
01 049dfc1c 65f034bc w3svc!FILTER_LIST::NotifyFilters(HTTP_FILTER* pFilter = 000003e8 , 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]
02 049dfc50 65f02cad w3svc!HTTP_FILTER::NotifyPreProcHeaderFilters(int* pfFinished = 049dfedc )+0x4c [.\filter.hxx @ 473]
03 049dfca8 65f02ac1 w3svc!HTTP_REQUEST::Parse(char* pchRequest = 06166e60 "GET /IISDebugLabs/site/fil_hc1.htm?sf=hc1_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]
04 049dfcec 65f029b5 w3svc!HTTP_REQ_BASE::OnCompleteRequest(char* pchRequest = 06166e60 "GET /IISDebugLabs/site/fil_hc1.htm?sf=hc1_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]
05 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]
06 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]
07 049dfec4 65f020d7 w3svc!HTTP_REQUEST::DoWork(int* pfFinished = 049dfedc )+0x99 [D:\nt\private\inet\iis\svcs\w3\server\httpreq.cxx @ 639]
08 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]
09 049dff08 65f01aa5 w3svc!CreateClient(_CLIENT_CONN_PARAMS* ClientParam = 049dff20 )+0x7b [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 310]
0a 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]
0b 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]
0c 049dffb4 77e887dd ISATQ!AtqPoolThread(unsigned long* param = abcdef01 )+0x1a8 [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 1127]
0d 049dffec 00000000 KERNEL32!BaseThreadStart(* lpStartAddress = 6d7091f7 , void* lpParameter = abcdef01 )+0x52 [D:\nt\private\windows\base\client\support.c @ 460]
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 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 571 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 571
}
}