Debug Labs


Heap Corruption/Double Free

This lab will demonstrate a heap corruption in a filter caused by a double free. 

 

1- Lab Setup
	

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

1- Verify the site is running in medium protection

2- Verify that the process "dllhost.exe" mapped to the out of process pooled application is running and note down its PID

To find the PID of the desired dllhost, do the follwing:

- Open Component Services

- Expand "Component Services" Node

- Expand "Computers" Node

- Expand "My Computer" Node

- Select "COM+ Applications"

- Click "Status View" icon and notice the PID column on the right hand pane

 

3- Attach windbg to the dllhost process with the PID found in step 2

a) Using the GUI:

- Click "File"

- Click "Attach to Process"

- Select "dllhost.exe" with the PID found in step 2 and click "OK"

b) Using the command prompt:

- Switch to thr folder where the debugging tools for windows is installed

- Type in "windbg -p <PID>" and hit Ente

4- 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/badext.dll)
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 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:006> kvn

# ChildEBP RetAddr Args to Child

00 00b1aff8 77fcbd67 00070000 0116c000 00b1b024 ntdll!RtlpCoalesceFreeBlocks+0x10f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 5556]

01 00b1b02c 77fcbbb3 00070000 00000208 00000100 ntdll!RtlpExtendHeap+0x193 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 5290]

02 00b1b1d4 77e88633 00070000 00000000 00000200 ntdll!RtlAllocateHeap+0x339 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 2054]

03 00b1b200 77ad99ea 00000000 00b1b21c 00000100 KERNEL32!GetModuleFileNameA+0x27 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\module.c @ 872]

04 00b1b31c 77b2d60d 00b1b34c 000ad3bc 00000006 ole32!AppInvokeExceptionFilter+0x5f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\excepn.cxx @ 124]

05 00b1fafc 77b29d6e 000b1c70 000ad3bc 000a0af8 ole32!SyncStubInvoke+0x85 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1138]

06 00b1fb44 77aa7ae7 000b1c70 000ad2d0 000a3a10 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]

07 00b1fba8 77aa7a33 0009e0b8 00000000 000a3a10 ole32!CCtxComChnl::ContextInvoke+0xbb (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 1204]

08 00b1fbc4 77b29c89 000b1c70 00000001 000a3a10 ole32!MTAInvoke+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 1952]

09 00b1fbf4 77b29a89 000b1c28 0009e0b8 000a3a10 ole32!AppInvoke+0xb5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1013]

0a 00b1fcb4 77b2a12c 000b3580 00000000 000ad3a0 ole32!ComInvokeWithLockAndIPID+0x29e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1578]

0b 00b1fcf4 77d33721 000b1c28 000ad3a0 00079af0 ole32!ThreadInvoke+0x1b7 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 3835]

0c 00b1fd2c 77d33667 77b29f7b 00079af0 00b1fe0c RPCRT4!DispatchToStubInC+0x84 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\dispatch.c @ 82]

0d 00b1fd84 77d33579 00000000 00000000 00b1fe0c RPCRT4!RPC_INTERFACE::DispatchToStubWorker+0x100 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 543]

0e 00b1fda4 77d48e81 00079af0 00000000 00b1fe0c RPCRT4!RPC_INTERFACE::DispatchToStub+0x5e (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 407]

0f 00b1fdd4 77d34bc6 00079af0 00079ab4 00000000 RPCRT4!RPC_INTERFACE::DispatchToStubWithObject+0xa9 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 795]

10 00b1fe10 77d346c5 000a4030 00081df8 80030001 RPCRT4!LRPC_SCALL::DealWithRequestMessage+0x1c6 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 2665]

11 00b1fe28 77d422ff 000799f8 00b1fe50 000a4030 RPCRT4!LRPC_ADDRESS::DealWithLRPCRequest+0x10c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 867]

12 00b1ff74 77d420d9 77d42528 00081df8 00000000 RPCRT4!LRPC_ADDRESS::ReceiveLotsaCalls+0x1eb (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 1175]

13 00b1ff78 77d42528 00081df8 00000000 00000000 RPCRT4!RecvLotsaCallsWrapper+0x9 (FPO: [1,0,0]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 37]

 

 

The faulting stack in a heap corruption issue will involve heap operations such as the one here " RtlpCoalesceFreeBlocks() " The first parameter passed to the finction is the

heap address.

 

Let see hoe heap " 00070000 " looks like:

 

0:006> ! heap 00070000 -m

Index Address Name Debugging options enabled

1: 00070000 Segment at 00070000 to 00170000 (00046000 bytes committed)

Segment at 01160000 to 01260000 (0000d000 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: 000006f8

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: 000705A8

FreeList Usage: 00000004 00000000 00000000 00000000

FreeList[ 00 ] at 00070178: 000b3890 . 000b3890

Unabled to read HEAP_ENTRY structure at 6262625a

FreeList[ 02 ] at 00070188: 000b0c88 . 000b05a0

Segment00 at 00070640:

Flags: 00000000

Base: 00070000

First Entry: 00070680

Last Entry: 00170000

Total Pages: 00000100

Total UnCommit: 000000ba

Largest UnCommit:00000000

UnCommitted Ranges: (1) 000b5000: 000bb000

unable to read heap entry at 000e1040

Segment01 at 01160000:

Flags: 00000000

Base: 01160000

First Entry: 01160040

Last Entry: 01260000

Total Pages: 00000100

Total UnCommit: 000000f3

Largest UnCommit:000f3000

UnCommitted Ranges: (1) 0116d000: 000f3000

 

 

 

So what this tells us is that the heap at " 00070000 " 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:001> g

Page heap: block @ 02C3CFF0 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

(880.87c): Break instruction exception - code 80000003 (first chance)

eax=00000000 ebx=00000027 ecx=77fb3d26 edx=00c9f2ae esi=02c3cfd0 edi=00511000

eip=77f97704 esp=00c9f49c ebp=00c9f4c8 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:001> kvn

# ChildEBP RetAddr Args to Child

00 00c9f498 77fb3de6 02c3cff0 77fb4248 02c3cff0 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]

01 00c9f4a0 77fb4248 02c3cff0 00000027 00511000 ntdll!RtlpDphReportCorruptedBlock+0x8c (FPO: [2,0,1]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4142]

02 00c9f4c8 77fb2f3f 00511000 01001002 02c3cff0 ntdll!RtlpDphNormalHeapFree+0x46 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4575]

03 00c9f4ec 77fb5887 00510000 01001002 02c3cff0 ntdll!RtlpDebugPageHeapFree+0xa6 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 2862]

04 00c9f564 77fa7111 00510000 01001002 02c3cff0 ntdll!RtlDebugFreeHeap+0x203 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 915]

05 00c9f604 77fcbeda 00510000 01001002 02c3cff0 ntdll!RtlFreeHeapSlowly+0x4d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3466]

06 00c9f6a8 7800115c 00510000 00000000 02c3cff0 ntdll!RtlFreeHeap+0x53 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3138]

07 00c9f6f0 02c716af 02c3cff0 0056dff0 78002041 MSVCRT!free+0xda (CONV: cdecl) [free.c @ 101]

08 00c9f708 02c7140f 022e6d70 00000001 022e6e3c badext!doHC2+0x31 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 281]

09 00c9f728 65d82b06 022e6d70 022e6e3c 022e6d70 badext!HttpExtensionProc+0x128 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 119]

0a 00c9f73c 65d82a97 022e6d70 022e6e3c 022e6d70 wam!HSE_APPDLL::ExecuteRequest+0x93 (FPO: [1,0,3]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 450]

0b 00c9f770 65d823a2 00569f70 022e6ef2 022e6d70 wam!WAM::InvokeExtension+0x35 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 913]

0c 00c9f7b4 77d339c0 005f3f80 00000000 000000c8 wam!WAM::ProcessRequest+0x240 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 643]

0d 00c9f7dc 77d93570 65d8223f 00c9f7f8 00000005 RPCRT4!Invoke+0x30 [..\i386\stubless.asm @ 88]

0e 00c9fa54 77d949ac 01a3efe0 01a66e98 019c0f28 RPCRT4!NdrStubCall2+0x63d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1724]

0f 00c9fab8 77b29e7f 01a3efe0 019c0f28 01a66e98 RPCRT4!CStdStubBuffer_Invoke+0xec (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\stub.c @ 1571]

10 00c9fafc 77b29d6e 019c0f28 0148af24 01106aa8 ole32!SyncStubInvoke+0x61 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @

1136] 11 00c9fb44 77aa7ae7 019c0f28 01486f38 01a3efe0 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]

12 00c9fba8 77aa7a33 01a66e98 00000000 01a3efe0 ole32!CCtxComChnl::ContextInvoke+0xbb (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 1204]

13 00c9fbc4 77b29c89 019c0f28 00000001 01a3efe0 ole32!MTAInvoke+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 1952]

 

 

 

 

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 281 looks like

 

DWORD doHC2(EXTENSION_CONTROL_BLOCK *lpEcb, 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 281

return (DWORD) ptr;

}

...

}

 

 

 

 

 


- Microsoft Confidential -