Debug Labs


Unhandled Exception in Component Code (Software  CONTINUABLE)

This lab will demonstrate an Unhandled Exception in a component. In this scenario, an Unhandled Exception in the CGI Executable "badCgi.exe".

This scenario is presented in 2 variations, the 1st variation shows the unhandled exception in the main thread "main" of the badCgi process, while the second variation shows the unhandled exception in a spawned thread other than the main thread.

In both variations, the Unhandled Exception is caused by a CONTINUABLE exception implemented by a call to RaiseException(...).

1- Lab Setup
	

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

1- Set the IIS Admin Service to interact with the Desktop, and verify the service is running under the system account.

- In "Administrative Tools" click "Services"

- Double Click "IIS Admin Service"

- Click the "Log On" Tab

- Verify that the radio button "Local System Account" is selected

- Check "Allow Services to interact with Desktop" checkbox

2- Set the "Image File Execution Options" for the CGI Executable "badCgi.exe"

- Start Registry Editor (Regedt32.exe).

- Locate the following registry key: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\]

- Create a subkey with the name "badCgi.exe"

- To the "badCgi.exe" subkey, add the REG_SZ value named Debugger and the full path to the debugger with the "-g" switch: eg: c:\debugger\windbg -g

 

3- Set the "CreateProcessAsUser" metabase prooperty to "false"

- Open the command prompt (cmd)

- Switch to "Inepub\Adminscripts" folder

- Type in: CScript.exe adsutil.vbs set w3svc/CreateProcessAsUser "false" and hit Enter

 

 

2- Run Scenario

1- Click the "Cause Unknown Exception V:1" button the first time to cause a 1st chance Unknown Exception in dllhost.exe.
 Note that the debugger breaks and diplays 
	a- Comments about the exception
b- The value of the registers
c- The module where the Eception happened
d- The assembly line that caused the Exception
2- To study the second variation of this lab, click the "Cause Unknown Exception V:2" button.

                                                             

1) Unhandled Exception in the main thread running HttpExtensionProc:  (Variation 1)

1- Debug Annotation

 

1) Without symbols

 

a) The faulting Stack

 

The stack backtrace of the thread that caused the Unhandled Exception would look like:

 

0:000> kvn50

*** ERROR: Module load completed but symbols could not be loaded for badcgi.exe

# ChildEBP RetAddr Args to Child

WARNING: Stack unwind information not available. Following frames may be wrong.

00 0006fe30 010019a8 00000057 00000000 00000000 KERNEL32!RaiseException+0x37

01 0006fe50 010015e5 0006fe6c 00000001 77f82b95 badcgi+0x19a8

02 0006ff70 01001ddf 00000001 00232468 00232cd8 badcgi+0x15e5

03 0006ffc0 77e9ca90 77f82b95 77f83920 7ffdf000 badcgi+0x1ddf

04 0006fff0 00000000 01001ce0 00000000 000000c8 KERNEL32!CreateProcessW+0x15a6

 

This call stack shows that an exception of code (00000057) was raised in "badCgi.exe". This exception is a software exception that translates to "ERROR_INVALID_PARAMETER". In this case,

the exception is CONTINUABLE (Second parameter of RaiseException is 0) which means that even if it not handled, execution could continue.

 

Keep in mind that we are here debugging a custom CGI application that misbehaved. The web application relying on this CGI will eventually not run properly.

So, in this stack backtrace, it is clear that the culprit is "badCgi.exe". So it is enough to tell the customer to contact the CGI vendor to fix it. If it is an in-house component and the customer would like help

from Microsoft, they should open an advisory case to troubleshoot the issue further.

 

 

2) With symbols including public symbols of "badCgi.exe"

 

First, we need to add the private symbol path(company wide symbols) as well as public symbols of our exe

a) Using the GUI:

- Click "File"

- Click "Symbol File Path"

- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pub and hit Enter

b) Using the ".sympath+" command:

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

 

0:000> kvn50

# ChildEBP RetAddr Args to Child

00 0006fe30 010019a8 00000057 00000000 00000000 KERNEL32!RaiseException+0x56 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\thread.c @ 1382]

01 0006fe50 010015e5 0006fe6c 00000001 77f82b95 badcgi!doRE1+0x18 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 460]

02 0006ff70 01001ddf 00000001 00232468 00232cd8 badcgi!main+0x265 (CONV: cdecl) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 166]

03 0006ffc0 77e9ca90 77f82b95 77f83920 7ffdf000 badcgi!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]

04 0006fff0 00000000 01001ce0 00000000 000000c8 KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]

 

 

Again, In this stack backtrace, it is clear that the culprit is "badCgi.exe". So it is enough to tell the customer to contact the dll vendor to fix it. If it is an in-house component and the customer would like help

from Microsoft, they should open an advisory case to troubleshoot the issue further.

 

 

 

To verify that the symbols are lined up properly for our extension, issue this command.

 

 

0:000> lmv mbadcgi

start end module name

01000000 01004000 badcgi (pdb symbols) c:\iisdebuglabs\symbols\pub\badcgi.pdb

Loaded symbol image file: badcgi.exe

Image path: C:\IISDebugLabs\www\app\badcgi.exe

Timestamp: Tue May 13 17:49:56 2003 (3EC16884) Checksum: 0000691D

 

 

3) with Private Symbols "badCgi.exe"

 

 

First, we need to add the private symbol path(company wide symbols) as well as public symbols of our exe

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

 

The stack backtrace of the thread that caused the Unhandled Exception would look like:

 

 

0:000> kvn50

# ChildEBP RetAddr Args to Child

00 0006fe30 010019a8 00000057 00000000 00000000 KERNEL32!RaiseException+0x56 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\thread.c @ 1382]

01 0006fe50 010015e5 0006fe6c 00000001 77f82b95 badcgi!doRE1+0x18 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 460]

02 0006ff70 01001ddf 00000001 00232468 00232cd8 badcgi!main+0x265 (CONV: cdecl) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 166]

03 0006ffc0 77e9ca90 77f82b95 77f83920 7ffdf000 badcgi!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]

04 0006fff0 00000000 01001ce0 00000000 000000c8 KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]  

 

Notice in frame 01 and 02 the line of the code as well as the name and location of the source code. Going back to the source code in badCgi.cpp:

 

Line 166 looks like

 

DWORD main( int argc, char * argv[])

{

....

....

else if (!strncmp(szS, "av1", 3))

{

return doRE1(Variation); //Line 166

}

....

....

 

return (1);

} // main()

 

 

Line 460 looks like

 

 

DWORD doRE1(int Variation)

{

DWORD dwThreadID;

HANDLE hThread;

if (1 == Variation)

{

RaiseException(ERROR_INVALID_PARAMETER, 0, 0, NULL); //line 460

}

if (2 == Variation)

{

HANDLE lpHandles[1];

lpHandles[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)REContinuable, NULL, 0, &dwThreadID);

DWORD dw = WaitForMultipleObjects(1, lpHandles, TRUE, INFINITE);  

}

return 1;

}

 

 

Advanced Technique

 

Remember that our web application is running in the IIS out of process pooled application (dllhost.exe).

The CGI executable is loaded in its own memory space and it is basically spawned by "inetinfo.exe". So for all http requests involving directly the the CGI

without passing via another extension will be processed by both inetinfo and the CGI executable without passing via dllhost. (for more information, please

refer to the class docs Chapter:...).

 

To get a feeling on how the CGI processing is made, let do the following:

 

1) In the command prompt, switch to the folder where the debugger is installed and run the following command:

c:\ debuggers>windbg -pn inetinfo.exe

 

2) In WinDbg, let set a breakpoint in CreateProcess function of Kernel32

In the windbg command line, type in "bp KERNEL32!CreateProcessA"

 

3) In Windbg command line, type in "g" and hit "Enter"

 

4) Click the "AV CGI" button in this page.

 

Notice that the debugger breaks, the command line is now available to run commands.

 

The stack backtrace of the thread where the break point was hit looks like:

 

0:010> kp

ChildEBP RetAddr

0136ee98 65f1be75 KERNEL32!CreateProcessA(char* lpApplicationName = 00000000 "", char* lpCommandLine = 0136f4b0 ""C:\IISDebugLabs\www\App\badCgi.exe" ", _SECURITY_ATTRIBUTES* lpProcessAttributes = 00000000 , _SECURITY_ATTRIBUTES* lpThreadAttributes = 00000000 , int bInheritHandles = 1, unsigned long dwCreationFlags = 0xc, void* lpEnvironment = 0136eed4 , char* lpCurrentDirectory = 0136fba8 "C:\IISDebugLabs\www", _STARTUPINFOA* lpStartupInfo = 0136f5b4 , _PROCESS_INFORMATION* lpProcessInformation = 0136f658 ) [D:\nt\private\windows\base\client\process.c @ 715]

0136f678 65f1b925 w3svc!HTTP_REQUEST::ProcessCGI(EXEC_DESCRIPTOR* pExec = 00e9eb2c , STR* pstrPath = 00000000 , STR* pstrWorkingDir = 0136fd10 , int* pfHandled = 0136feb0 , int* pfFinished = 0136fedc , STR* pstrCmdLine = 0136f5fc )+0x4c3 [D:\nt\private\inet\iis\svcs\w3\server\cgi.cxx @ 1179]

0136fd48 65f2a610 w3svc!HTTP_REQUEST::ProcessGateway(EXEC_DESCRIPTOR* pExec = 00e9eb2c , int* pfHandled = 0136feb0 , int* pfFinished = 0136fedc , int fTrusted = 0)+0x34e [D:\nt\private\inet\iis\svcs\w3\server\cgi.cxx @ 919]

0136fec4 65f020d7 w3svc!HTTP_REQUEST::DoWork(int* pfFinished = 0136fedc )+0x43f [D:\nt\private\inet\iis\svcs\w3\server\httpreq.cxx @ 839]

0136fee4 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]

0136ff08 65f01aa5 w3svc!CreateClient(_CLIENT_CONN_PARAMS* ClientParam = 0136ff20 )+0x7b [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 310]

0136ff4c 6d701443 w3svc!W3OnConnectEx(void* patqContext = 012742d8 , unsigned long cbWritten = 0xe4, unsigned long err = 0x1131e8, _OVERLAPPED* lpo = 00e897b8 )+0x118 [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 215]

0136ff80 6d702203 ISATQ!AtqpProcessContext(ATQ_CONTEXT* pAtqContext = 00000000 , unsigned long cbWritten = 0xe4, _OVERLAPPED* lpo = 012742dc , int fRet = 1)+0x23a [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 883]

0136ffb4 77e887dd ISATQ!AtqPoolThread(unsigned long* param = abcdef01 )+0x1a8 [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 1127]

0136ffec 00000000 KERNEL32!BaseThreadStart(* lpStartAddress = 6d7091f7 , void* lpParameter = abcdef01 )+0x52 [D:\nt\private\windows\base\client\support.c @ 460]

 

Note that the second parameter to CreateProcess is the command line to trun that process which is in this case "C:\IISDebugLabs\www\App\badCgi.exe"

 

Let try to get the whole HTTP request that w3svc received.

 

For this, let dump the EXEC_DESCRIPTOR class.

 

0:010> dt EXEC_DESCRIPTOR 00e9eb2c

+0x000 _pstrURL : 0x00e9e370

+0x004 _pstrPhysicalPath : 0x00e9e5e0

+0x008 _pstrUnmappedPhysicalPath : 0x00e9e610

+0x00c _pstrGatewayImage : 0x00e9eac0

+0x010 _pstrPathInfo : 0x00e9e400

+0x014 _pstrURLParams : 0x00e9e3d0

+0x018 _pdwScriptMapFlags : 0x00e9eabc -> 0

+0x01c _pGatewayType : 0x00e9e060 300002 ( GATEWAY_CGI )

+0x020 _pRequest : 0x00e9e008

+0x024 _hChildEvent : (null)

+0x028 _fMustWaitForChildEvent : 0 +0x02c _pParentWamRequest : (null)

+0x030 _dwExecFlags : 0

+0x034 _pMetaData : 0x00e9ecd8

+0x038 _pPathInfoMetaData : (null)

+0x03c _pPathInfoURIBlob : (null)

+0x040 _pAppPathURIBlob : (null)

 

 

Now let dump the HTTP_REQUEST class.

 

0:010> dt HTTP_REQUEST 0x00e9e008

+0x000 __VFN_table : 0x65f08680

+0x008 _cFilesSent : 0

+0x00c _cFilesReceived : 0

......

......

+0x9a8 _bufClientRequest : BUFFER

......

......

+0xbac _pFileNameLock : (null)

+0xbb0 _fPossibleDefaultExecute : 0y0

=65f465b0 HTTP_REQUEST::sm_GetInfoFuncs : [26] 0x65f26a87 w3svc!HTTP_REQUEST::GetInfoA+0

+0xbb4 _strDLCString : STR

 

 

The "_bufClientRequest " points to the buffer where the HTTP request is stored.

 

0:010> da poi(0x00e9e008 + 0x9a8 )

006ae398 "GET /IISDebugLabs/App/badCgi.exe"

006ae3b8 "?s=av1_v1 HTTP/1.1..Accept: */*."

006ae3d8 ".Accept-Language: en-us..Accept-"

006ae3f8 "Encoding: gzip, deflate..User-Ag"

006ae418 "ent: Mozilla/4.0 (compatible; MS"

006ae438 "IE 6.0; Windows NT 5.0)..Host: l"

006ae458 "ocalhost..Connection: Keep-Alive"

006ae478 "...."

 

5) In the windbg command line, type in "g" and hit "Enter"

Notice a second windbg window is displayed, this is the one attached to the CGI executable "badCgi.exe".

 

6) In the Windbg instance attached to "badCgi.exe" type in "q" and hit "Enter".

 

7) Finaly, back to the main WinDbg window, press "Ctrl-Break" to break in to the debugger, type in "q" and hit "Enter"

 

 

1) Unknown Exception in a spawned thread: (Variation 2)

1- Debug Annotation

 

1) Without symbols

 

a) The faulting Stack

 

The stack backtrace of the thread that caused the Unhandled Exception would look like:

 

0:001> kvn50

*** ERROR: Module load completed but symbols could not be loaded for badcgi.exe

# ChildEBP RetAddr Args to Child

WARNING: Stack unwind information not available. Following frames may be wrong.

00 0028ffa0 01001a36 00000057 00000000 00000000 KERNEL32!RaiseException+0x37

01 0028ffec 00000000 01001a29 00000000 00000000 badcgi+0x1a36  

 

 

In this stack backtrace, it is clear that the culprit is "badCgi.exe". So it is enough to tell the customer to contact the dll vendor to fix it. If it is an in-house component and the customer would like help

from Microsoft, they should open an advisory case to troubleshoot the issue further.

 

b) The regiters values

 

0:001> r

eax=0028ff50 ebx=00000000 ecx=00000101 edx=ffffffff esi=00000000 edi=77fb5282

eip=77e9e8bb esp=0028ff48 ebp=0028ffa0 iopl=0 nv up ei pl zr na po nc

cs=001b ss=0023 ds=0023 es=0023 fs=0038 gs=0000 efl=00000246

KERNEL32!RaiseException+37:

77e9e8bb e988c60100 jmp KERNEL32!GetThreadPriorityBoost+0x105 (77ebaf48)  

 

c) The disassmbly code

 

0:001> u eip

KERNEL32!RaiseException+37:

77e9e8bb e988c60100 jmp KERNEL32!GetThreadPriorityBoost+0x105 (77ebaf48)

77e9e8c0 8b4510 mov eax,[ebp+0x10]

77e9e8c3 83f80f cmp eax,0xf

77e9e8c6 0f8774c60100 jnbe KERNEL32!GetThreadPriorityBoost+0xfd (77ebaf40)

77e9e8cc 8945c0 mov [ebp-0x40],eax

77e9e8cf 8bc8 mov ecx,eax

77e9e8d1 48 dec eax

77e9e8d2 85c9 test ecx,ecx

 

 

2) With symbols including public symbols of "badCgi.exe"

 

First, we need to add the public symbol path(company wide symbols) as well as symbols of our dll

a) Using the GUI:

- Click "File"

- Click "Symbol File Path"

- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pub and hit Enter

b) Using the ".sympath+" command:

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

 

The stack backtrace of the thread that caused the Unhandled Exception would look like:

 

 

 

0:001> kvn50

# ChildEBP RetAddr Args to Child

00 0028ffa0 01001a36 00000057 00000000 00000000 KERNEL32!RaiseException+0x56 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\thread.c @ 1382]

01 0028ffec 00000000 01001a29 00000000 00000000 badcgi!REContinuable+0xd

 

 

Now, with the public symbols lined up, we see that the function call that raised the exception is to "ReContinuable"

 

Again, In this call stack, it is clear that the culprit is "badCgi.exe". So it is enough to tell the customer to contact the dll vendor to fix it. If it is an in-house component and the customer would like help

from Microsoft, they should open an advisory case to troubleshoot the issue further.

 

 

To verify that the symbols are lined up properly for our extension, issue this command.

 

 

0:000> lmv mbadcgi

start end module name

01000000 01004000 badcgi (pdb symbols) c:\iisdebuglabs\symbols\pub\badcgi.pdb

Loaded symbol image file: badcgi.exe

Image path: C:\IISDebugLabs\www\app\badcgi.exe

Timestamp: Tue May 13 17:49:56 2003 (3EC16884) Checksum: 0000691D

 

 

3) with Private Symbols "badCgi.exe"

The stack backtrace of the thread that caused the Unhandled Exception would look like:

 

 

0:001> kvn50

# ChildEBP RetAddr Args to Child

00 0028ffa0 01001a36 00000057 00000000 00000000 KERNEL32!RaiseException+0x56 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\thread.c @ 1382]

01 0028ffec 00000000 01001a29 00000000 00000000 badcgi!REContinuable+0xd (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 504]

 

     

Notice in frame 01 the line of the code as well as the name and location of the source code. Going back to the source code in badCgi.exe.cpp:

 

Line 504 looks like

 

DWORD REContinuable()

{

RaiseException(ERROR_INVALID_PARAMETER, 0, 0, NULL); //line 504

return 1;

}

 


- Microsoft Confidential -