Issue the following command in the Wndbg Command line:
0:000> !handle
Handle 4
Name <none>
Handle 8
Name <none>
Handle c
Name <none>
.......
219 Handles
Type Count
Event 15
Section 4
File 183
Port 1
Directory 3
Mutant 3
WindowStation 2
Semaphore 2
Process 1
Thread 2
Desktop 1
IoCompletion 2
In windbg, type in "g", notice that the a second breakpoint is hit (this is because the process is exiting)
0:000> !handle
0:000> !handle
Handle 4
Name <none>
Handle 8
Name <none>
Handle c
Name <none>
.......
271 Handles
Type Count
Event 15
Section 4
File 183
Port 1
Directory 3
Mutant 3
WindowStation 2
Semaphore 2
Key 52
Process 1
Thread 2
Desktop 1
IoCompletion 2
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 0 (No registry key handle
at startup) to 52.
Issue again the "!handle 0 4" to include the handle type information in the display
0:031> !handle 0 4
.........
.........
.........
Handle 9c
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle a0
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle a4
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.
- Click the "Handle Leak" button one time
- In Windbg command line, issue the following command:
0:000> 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"
- Notice that the debugger breaks.
Issue a "kvn" in the debugger's command line.
Having the private symbols lined up, here is how the stack would look like:
0:000> g
Breakpoint 0 hit
eax=00000000 ebx=00000003 ecx=0006ffb0 edx=00000001 esi=0006fd8c edi= 00000031
eip=77db5fc3 esp=0006fd68 ebp=0006fe50 iopl=0 nv up ei pl nz na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=0038 gs=0000 efl= 00000202
ADVAPI32!RegOpenKeyExA:
77db5fc3 55 push ebp
0:000> kvn
# ChildEBP RetAddr Args to Child
00 0006fd64 0100193f 80000002 01001240 00000000 ADVAPI32!RegOpenKeyExA (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\screg\winreg\client\regkey.c @ 875]
01 0006fe50 01001580 0006fe6c 00000001 000845b0 badCGI!doHandleLeak1+0x2f (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 423]
02 0006ff70 01001f1f 00000001 00bd8fd0 00350f40 badCGI!main+0x1f0 (CONV: cdecl) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 154]
03 0006ffc0 77e9ca90 000845b0 00000000 7ffdf000 badCGI!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]
04 0006fff0 00000000 01001e20 00000000 eeeeeeee KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]
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:000> da 01001240
01001240 "SYSTEM\CurrentControlSet\Control"
001001260 "\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 423 looks like in "badCgi.cpp"
DWORD doHandleLeak1(CHAR szQS[DEFAULT_LEN], int Variation)
{
if (1 == Variation)
{
const showLeakedH = 50;
HKEY hKey[showLeakedH];
LONG lRet; //this is to show the leak of 50 handles in a single request.
for (int i = 0; i < showLeakedH; i++)
{
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
0, KEY_QUERY_VALUE, &hKey[i] ); // line 423
}
}
return 1;
}