Debug Labs


Handle Leak in Component

This lab will demonstrate a handle leak. In this scenario, a handle leak in the ISAPI Extension "badext.dll"

 

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- Run Performace Monitor to monitor the process handle count.

a- Start Performance Monitor

b- Click the Add button

c- Under "Performance Object", Select "Process"

d- Under "Select Counters from List", select "Handle Count"

e- Under "Select Instances from List", select "dllhost#1"

 

4- 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 Enter

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

 

 

2- Run Scenario

1- Click the "Invoke Extension" button to load the ISAPI Extension in the dllhost process memory space. You should see the ISAPI extension "badExt.dll"among the list of modules loaded in dllhost.exe
2- Click the "Handle Leak" button many times to cause a handle leak.
 Note that in Performance Monitor, the "handle count" counter increases and never decreases. This is an indication that there is a handle leak. 
 3- Back in Windbg, press "Ctrl_Break". 

					

                                                                           

 

1- Debug Annotation

 

Once you start Performance Monitor to monitor handles count, note how many handles the process "dllhost#1" has.

In our scenario, it shows 434 handles. Verify this count in windbg right after you attach by issuing this command:

 

0:031> !handle

Handle 4

Type Section

Handle 8

Type Event

Handle c

Type Event

.......

434 Handles

Type Count

Event 215

Section 8

File 87

Port 15

Directory 3

Mutant 6

WindowStation 2

Semaphore 20

Key 25

Token 2

Thread 43

Desktop 1

IoCompletion 6

Timer 1

 

 

In windbg, type in "g", and back to Internet Explorer, keep pressing "Handle Leak" button. (or just paste the URL that causes the leak in a new

IE window and keep pressing F5). This is the URL "http://localhost/badext.dll?s=handleleak1_v1"

 

Now performance Monitor shows 717 handles open. Back to Windbg, press "Ctrl_break" and run the "!handle" command again.

Here is the output

In Windbg

 

0:031> !handle

Handle 4

Type Section

Handle 8

Type Event

Handle c

Type Event

.......

717 Handles

Type Count

Event 217

Section 8

File 87

Port 16

Directory 3

Mutant 6

WindowStation 2

Semaphore 20

Key 304

Token 3

Thread 43

Desktop 1

IoCompletion 6

Timer 1

 

Based on this, we can see that the handle that we are leaking is a handle to a registry key. the key handle count jumped from 25 to 304.

Issue again the "!handle 0" to include the handle type information in the display

 

0:031> !handle 0

.........

.........

.........

 

Handle 650

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

Handle 654

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

Handle 658

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

Handle 65c

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

Handle 660

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

Handle 664

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

Handle 668

Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions

.............

.............

.............

 

 

So this shows that the handle we are leaking is to he registry key "\REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions"

Now let try to see who is leaking the handle to the registry key mentioned above. To do this, we are going to set a break point.

 

In windbg command line, type in this command:

 

0:003> bp ADVAPI32!RegOpenKeyExA

 

This will set a break point in RegOpenKeyExA. When the break point is hit, the debugger breaks.

 

In Windbg command line, type in "g"

In the browser, click  the "Leak Handle" button, and notice that the debugger breaks.

Issue a "kvn50" in the debugger's command line.

 

 

Add the symbol path(company wide symbols) as well as private symbols of our dll

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:

.sympath+ SRV*C:\LabSymbols*\\symbols\symbols;C :\IISDebugLabs\symbols\Pri

Having the private symbols lined up, here is how the stack would look like:

 

0:006> kvn50

# ChildEBP RetAddr Args to Child

00 00b3f6f0 012917a5 80000002 0129119c 00000000 ADVAPI32!RegOpenKeyExA (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\screg\winreg\client\regkey.c @ 875]

01 00b3f70c 012914b0 00e02128 00000001 00e021f4 badext!doHandleLeak1+0x21 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 337]

02 00b3f728 65d82b06 00e02128 00e021f4 00e02128 badext!HttpExtensionProc+0x18d (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 124]

03 00b3f73c 65d82a97 00e02128 00e021f4 00e02128 wam!HSE_APPDLL::ExecuteRequest+0x93 (FPO: [1,0,3]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 450]

04 00b3f770 65d823a2 0040a8c8 00e0229d 00e02128 wam!WAM::InvokeExtension+0x35 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 913]

05 00b3f7b4 77d339c0 00406c08 00000000 000000a6 wam!WAM::ProcessRequest+0x240 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 643]

06 00b3f7dc 77d93570 65d8223f 00b3f7f8 00000005 RPCRT4!Invoke+0x30 [..\i386\stubless.asm @ 88]

07 00b3fa54 77d949ac 000c8f78 0009fd94 000b5660 RPCRT4!NdrStubCall2+0x63d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1724]

08 00b3fab8 77b29e7f 000c8f78 000b5660 0009fd94 RPCRT4!CStdStubBuffer_Invoke+0xec (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\stub.c @ 1571]

09 00b3fafc 77b29d6e 000b5660 000c832c 000a25a8 ole32!SyncStubInvoke+0x61 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1136]

0a 00b3fb44 77aa7ae7 000b5660 000c8240 000c8f78 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]

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

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

0d 00b3fbf4 77b29a89 000b5618 0009fd94 000c8f78 ole32!AppInvoke+0xb5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1013]

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

0f 00b3fcf4 77d33721 000b5618 000c8310 0007a410 ole32!ThreadInvoke+0x1b7 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 3835]

10 00b3fd2c 77d33667 77b29f7b 0007a410 00b3fe0c RPCRT4!DispatchToStubInC+0x84 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\dispatch.c @ 82]

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

12 00b3fda4 77d48e81 0007a410 00000000 00b3fe0c RPCRT4!RPC_INTERFACE::DispatchToStub+0x5e (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 407]

13 00b3fdd4 77d34bc6 0007a410 0007a3d4 00000000 RPCRT4!RPC_INTERFACE::DispatchToStubWithObject+0xa9 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 795]

14 00b3fe10 77d346c5 000a4d40 000826a0 80030001 RPCRT4!LRPC_SCALL::DealWithRequestMessage+0x1c6 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 2665]

15 00b3fe28 77d422ff 0007a318 00b3fe50 000a4d40 RPCRT4!LRPC_ADDRESS::DealWithLRPCRequest+0x10c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 867]

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

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

18 00b3ffa8 77d424da 0009d728 00b3ffec 77e887dd RPCRT4!BaseCachedThreadRoutine+0x4f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 3586]

19 00b3ffb4 77e887dd 0009ee40 00000000 00000000 RPCRT4!ThreadStartRoutine+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\threads.cxx @ 230]

1a 00b3ffec 00000000 77d424c2 0009ee40 00000000 KERNEL32!BaseThreadStart+0x52 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 460]

 

 

Let see what registry we are trying to open here:

The 2nd parameter of (ADVAPI32!RegOpenKeyExA ) is actually a pointer to a null teminated string that hold the registry key name.

Dumping that address shows the following:

 

0:006> da 0129119c

0129119c "SYSTEM\CurrentControlSet\Control"

012911bc "\ProductOptions"

 

So the handle to this key is actually what we are leaking. From the stack backtrace, you could see that (ADVAPI32!RegOpenKeyExA ) is called from (badext!doHandleLeak1). This is enough to tell teh customer that the

component "badext.dll" is the one causing the handle leak.

 

Here is how line 337 looks like in "badext.cpp"

 

DWORD doHandleLeak1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation)

{

if (1 == Variation)

{

HKEY hKey;

LONG lRet;

 

lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); //line 337

}

return HSE_STATUS_SUCCESS;

}

 


- Microsoft Confidential -