Tag Archive for 'Handle Leak'

[debugging tip]!htrace에서 잡지못하는 handle leak..

Windbg로 Handle Leak을 잡을때 대부분 !htrace라는 녀석을 사용합니다. !htrace는 Application Verifier의 Detect invalid handle기능을 사용하기 때문에 굉장히 신뢰도가 높습니다. 하지만 몇몇 함수들에서 발생하는 Handle Leak은 Snapshot을 만들지 못하기 때문에 잡을 수 없는 경우가 있지요.

대표적인 예의 함수가 RegCreateKeyEx함수 입니다.

LONG WINAPI RegCreateKeyEx(
__in HKEY hKey,
__in LPCTSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPTSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
);

RegCreateKeyEx 함수의 경우 8번째 Parameter로 Key Handle을 Return 받을 수 있는데 이 함수를 통해서 Return 받은 Handle에 대해서는 !trace로 Snapshot이 불가능합니다. 그 이유는 !htrace Extension의 경우 OCP(Open-Close Principle) Function에 한해서 Snapshot을 만들기 때문이죠. 가령 Registry의 경우는 RegOpenKey, RegCloseKey와 같이 OCP쌍을 이루는 Function의 Snapshot을 추적합니다.

혹시나 !htrace Extension으로도 발견하지 못하는 handle Leak이 있다면 Create 관련 Function에서 Return 되는 Handle을 의심해 보는 것도 하나의 방법이 될꺼라 생각되내요

[debugging tip] Windows 2000 Section Handle Leak

Windows 2000의 경우 Windbg에서 제공하는 !htrace Extension을 사용할 수 없기에 Handle Leak를 검출하기는데 어려움이 있지요. 특히나 Process Explorer와 같은 툴에서 Handle Data는 증가하지 않는데 Handle Count만 증가 하고 있다면 더더욱 힘든 상황입니다.  이러한 경우라면 nonamed Section Handle의 존재 여부를 확인해 보면 도움이 됩니다. Section Handle의 경우 각각의 Name Value를 가지는게 보통이지만 간혹 Leak을 발생시키는 경우라면 Name이 없는 경우가 있습니다.
0:002> !handle
Handle 4
Type Section
Handle 8
Type Event
Handle c
Type Event
Handle 10
Type Event
Handle 14
Type Directory
Handle 18
Type File
Handle 1c
Type Directory
Handle 20
Type Event
Handle 24
Type Port
Handle 28
Type Mutant
Handle 2c
Type Event
Handle 30
Type Section
... 중략

Handle 2464
Type Section
Handle 2468
Type Section
Handle 246c
Type Section
Handle 2470
Type Section
Handle 2474
Type Section
Handle 2478
Type Section
Handle 247c
Type Section
Handle 2480
Type Section
Handle 2484
Type Section
Handle 2488
Type Section
Handle 248c
Type Section
Handle 2490
Type Section
Handle 2494
Type Section
Handle 2498
Type Section
Handle 249c
Type Section
Handle 24a0
Type Section
Handle 24a4
Type Section
Handle 24a8
Type Section
Handle 24ac
Type Section
Handle 24b0
Type Section
Handle 24b4
Type Section
Handle 24b8
Type Section
Handle 24bc
Type Section
Handle 24c0
Type Section
Handle 24c4
Type Section
Handle 24c8
Type Section
Handle 24cc
Type Section
Handle 24d0
Type Section
Handle 24d4
Type Section
Handle 24d8
Type Section
Handle 24dc
Type Section
Handle 24e0
Type Section
Handle 24e4
Type Section
Handle 24e8
Type Section
Handle 24ec
Type Section
Handle 24f0
Type Section
Handle 24f4
Type Section
Handle 24f8
Type Section
Handle 24fc
Type Section
Handle 2500
Type Section
Handle 2504
Type Section
Handle 2508
Type Section
Handle 250c
Type Section
Handle 2510
Type Section
Handle 2514
Type Section
Handle 2518
Type Section
2374 Handles
Type Count
Event 24
Section 2287
File 40
Port 1
Directory 3
Mutant 5
WindowStation 2
Semaphore 1
Key 6
Thread 2
Desktop 1
IoCompletion 2

0:002> !handle 8c ff
Handle 8c
Type File
Attributes 0
GrantedAccess 0x12019f:
ReadControl,Synch
Read/List,Write/Add,Append/SubDir/CreatePipe,
ReadEA,WriteEA,ReadAttr,WriteAttr
HandleCount 2
PointerCount 4
0:002> !handle 88 ff
Handle 88
Type Section
Attributes 0
GrantedAccess 0xf0007:
Delete,ReadControl,WriteDac,WriteOwner
Query,MapWrite,MapRead
HandleCount 2
PointerCount 4
Name \BaseNamedObjects\AtlDebugAllocator_FileMappingNameStatic3_22c
Object Specific Information
Section base address 0
Section attributes 0×4000000
Section max size 0×400000
0:002> !handle 2518 ff
Handle 2518
Type Section
Attributes 0
GrantedAccess 0xf0007:
Delete,ReadControl,WriteDac,WriteOwner
Query,MapWrite,MapRead
HandleCount 2
PointerCount 3
Name <none>
Object Specific Information
Section base address 0
Section attributes 0×8000000
Section max size 0×7000

Section Handle에는 보통 name이 있는게 정상이지만 Leak이 발생하고 있는 Handle에는 Name이 없더군요. 이러한 경우는 Windows 2000에서 WTS* API를 사용할 경우 주로 발생하고 있습니다.  혹시나 비슷한 문제가 있다면 한번 의심해보시길…