Debug Labs


 Heap Corruption/Old Pointer Reuse

This lab will demonstrate a heap corruption in a filter caused by reusing an already freed block. 

 

1- Lab Setup
	

Note: This lab assumes that you have already installed the necessary lab files to run it.

1-Install the ISAPI Filter

 

- Right Click the Server Name

- Click "Properties"

- In the "WWW Service" Master Properties, click "Edit"

- In the "ISAPI Filters" tab, click "Add"

- In the "Filter Name" type in "BadFilter"

- In the "Executable", browse to C:\IISDebugLabs\www\App\badfil.dll

- Click "Apply" and click "Ok" all teh way out.

 

2- Configure the VDir "IISDebugLabs" to run in low isolation (We need this to run an ASP page in the same memory space

as inetinfo.exe to put some stress on the process to cause heap corruption)

- In ISM, right click "IISDebugLabd" Vdir

- Click Properties

- In the "Application Protection" select "Low (IIS Process)"

- Click Apply and Click OK.

 

3- Restart the IIS Admin service

- Right Click the Server Name

- Click "Restart IIS" and Click "Ok"

 

4- Attach the debugger to inetinfo.exe

a) Using the GUI:

- Click "File"

- Click "Attach to Process"

- Select "inetinfo.exe" and click "OK"

b) Using the command prompt

-Type in "windng -pn inetinfo.exe" without quotation marks and hit "Enter"

 

5- In windbg command line, type in "g" with no quotation marks and press Enter

 

 

2- Run Scenario
 
1- Click the "Corrupt Heap" button 2 or 3 times. 
2- Open a new instance of Internet Explorer, Copy and paste the following URL to the address bar of the browser
(http://localhost/iisdebuglabs/app/initcom.asp)
3) Keep pressing  "F5" until the debugger breaks
 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
4- In the WinDbg command line, type in "g" and press "Enter", a 2nd chance AV is now encountered 

                 

 

1- Debug Annotation

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 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:008> kvn50

# ChildEBP RetAddr Args to Child

00 012afc18 750317a8 00070000 00000000 00000058 ntdll!RtlAllocateHeap+0x291 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 1703]

01 012afc28 75031bcb 00000058 00094438 012afc78 WS2_32!operator new+0x12 (FPO: [1,0,0]) (CONV: cdecl) [D:\nt\private\net\sockets\winsock2\dll\winsock2\wsautil.cpp @ 1632]

02 012afc38 750311f7 00094438 012afc78 00091d20 WS2_32!DTHREAD::CreateDThreadForCurrentThread+0x17 (FPO: [2,0,2]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\dll\winsock2\dthread.cpp @ 184]

03 012afc4c 7503195c 012afc8c 012afc78 00091d20 WS2_32!Prolog_v1+0x35 (FPO: [2,0,2]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\dll\winsock2\wsautil.cpp @ 167]

04 012afca8 6d701876 00000548 0000ffff 0000700b WS2_32!setsockopt+0x36 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\net\sockets\winsock2\dll\winsock2\sockctrl.cpp @ 588]

05 012afcd0 65f06720 00000000 00000001 77e87684 ISATQ!AtqCloseSocket+0x83 (FPO: [EBP 0x012aff08] [2,0,4]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\infocomm\atq\atqmain.cxx @ 1436]

06 012aff08 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]

07 012aff38 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]

08 012aff4c 6d701394 00e93ba8 00001087 00000000 w3svc!W3Completion+0x43 (FPO: [4,0,1]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\w3\server\conn.cxx @ 1857]

09 012aff80 6d702203 00000000 00001087 011f42dc ISATQ!AtqpProcessContext+0x262 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 912]

0a 012affb4 77e887dd abcdef01 00000000 77fcb650 ISATQ!AtqPoolThread+0x1a8 (FPO: [EBP 0x012affec] [1,4,4]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 1127]

0b 012affec 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:008> !heap 00070000 -m

Index Address Name Debugging options enabled

1: 00070000

Segment at 00070000 to 00170000 (000c6000 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: 0000016e

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 00000020

FreeList[ 00 ] at 00070178: 00108908 . 00108908

FreeList[ 65 ] at 000704a0: 00135ce0 . 00135ce0

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: 0000003a

Largest UnCommit:0003a000

UnCommitted Ranges: (1)

00136000: 0003a000

unable to read heap entry at 001495e8

 

 

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:008> g

Page heap: block @ 06412FF0 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

(768.6ac): Break instruction exception - code 80000003 (first chance)

eax=00000000 ebx=00000027 ecx=77fb3d26 edx=049df182 esi=06412fd0 edi=00341000

eip=77f97704 esp=049df370 ebp=049df39c 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:008> kpn

# ChildEBP RetAddr

00 049df36c 77fb3de6 ntdll!DbgBreakPoint [..\i386\debug2.asm @ 42]

01 049df374 77fb4248 ntdll!RtlpDphReportCorruptedBlock(void* Block = 06412ff0 , unsigned long Reason = 0x27)+0x8c [D:\nt\private\ntos\rtl\heappage.c @ 4142]

02 049df39c 77fb2f3f ntdll!RtlpDphNormalHeapFree(_DPH_HEAP_ROOT* Heap = 00341000 , unsigned long Flags = 0x1001002, void* Block = 06412ff0 )+0x46 [D:\nt\private\ntos\rtl\heappage.c @ 4575]

03 049df3c0 77fb5887 ntdll!RtlpDebugPageHeapFree(void* HeapHandle = 00340000 , unsigned long Flags = 0x1001002, void* Address = 06412ff0 )+0xa6 [D:\nt\private\ntos\rtl\heappage.c @ 2862]

04 049df438 77fa7111 ntdll!RtlDebugFreeHeap(void* HeapHandle = 00340000 , unsigned long Flags = 0x1001002, void* BaseAddress = 06412ff0 )+0x203 [D:\nt\private\ntos\rtl\heapdbg.c @ 915]

05 049df4d8 77fcbeda ntdll!RtlFreeHeapSlowly(void* HeapHandle = 00340000 , unsigned long Flags = 0x1001002, void* BaseAddress = 06412ff0 )+0x4d [D:\nt\private\ntos\rtl\heap.c @ 3466]

06 049df57c 7800115c ntdll!RtlFreeHeap(void* HeapHandle = 00340000 , unsigned long Flags = 0, void* BaseAddress = 06412ff0 )+0x53 [D:\nt\private\ntos\rtl\heap.c @ 3138]

07 049df5c4 05a01bea MSVCRT!free(void* pBlock = 06412ff0 )+0xda [free.c @ 101]

08 049dfbf4 65f0354f badfil!doHC3(int Variation = 0x65f0354f)+0x2b [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\filDBG\badfil.cpp @ 647]

09 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]

0a 049dfc50 65f02cad w3svc!HTTP_FILTER::NotifyPreProcHeaderFilters(int* pfFinished = 049dfedc )+0x4c [.\filter.hxx @ 473]

0b 049dfca8 65f02ac1 w3svc!HTTP_REQUEST::Parse(char* pchRequest = 06169e60 "GET /IISDebugLabs/site/fil_hc3.htm?sf=hc3_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 = 06169e60 "GET /IISDebugLabs/site/fil_hc3.htm?sf=hc3_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 647 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); //line 647

*ptr = 'x';

return (DWORD) ptr;

}

...

}

 

 

 

 

 


- Microsoft Confidential -