Debug Labs


Divide-By-Zero Exception in Component Code  

This lab will demonstrate an Integer Divide-By-Zero exception in a component. In this scenarion, a DBZ Exception in the CGI Executable  "badCGI.exe".

This scenario is presented in 2 variations, the 1st variation shows the exception in the main thread running the CGI program, while the second variation shows the exception in a spawned thread other than the main thread.

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 DBZ Exception V:1" button the first time to cause a 1st chance Integer Divide-By-Zero Exception in badCgi.exe
 Note that the windbg window will appear with the following information. 
	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

2- In windbg command line, type in "g" and hit enter to cause a 2nd chance AV.

 

3- To study the second variation of this lab, click the "Cause DBZ Exception V:2" button.

 

                                                                       

 

1) Integer Divide-By-Zero 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 Divide-ByZero Exception would look like:

 

 

 

0:000> kvn50 #

ChildEBP RetAddr Args to Child WARNING: Stack unwind information not available. Following frames may be wrong.

00 0006fe50 010015c4 0006fe6c 00000001 000851c8 badcgi+0x1956

01 0006ff70 01001daf 00000001 00232468 00232cd8 badcgi+0x15c4

02 0006ffc0 77e9ca90 000851c8 00000000 7ffdf000 badcgi+0x1daf

03 0006fff0 00000000 01001cb0 00000000 000000c8 KERNEL32!BaseProcessStart+0x3d

 

 

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.

 

b) The regiters values

 

0:000> r

eax=00000001 ebx=00000003 ecx=00000000 edx=00000000 esi=78002041 edi=002324b0

eip=01001956 esp=0006fe50 ebp=0006fe50 iopl=0 nv up ei pl zr na po nc

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

badcgi+1956:

01001956 f7f9 idiv ecx

 

c) The disassmbly code

 

0:000> u eip

badcgi+1956:

01001956 f7f9 idiv ecx

01001958 eb1e jmp badcgi+0x1978 (01001978)

0100195a 837d0c02 cmp dword ptr [ebp+0xc],0x2

0100195e 7515 jnz badcgi+0x1975 (01001975)

01001960 8d450c lea eax,[ebp+0xc]

01001963 50 push eax

01001964 33c0 xor eax,eax

01001966 50 push eax

 

 

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

b) Using the ".sympath+" command:

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

 

 

0:000> kvn

# ChildEBP RetAddr Args to Child

00 0006fe50 010015c4 0006fe6c 00000001 000851c8 badcgi!doDZ1+0xf

01 0006ff70 01001daf 00000001 00232468 00232cd8 badcgi!main+0x244

02 0006ffc0 77e9ca90 000851c8 00000000 7ffdf000 badcgi!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]

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

 

 

 

Now, with the public symbols lined up, we see that the last call that Caused the DBZ exception is "doDZ1" that was called from the CGI entry point (main)

 

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 private 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 DBZ would look like:

 

 

 

0:000> kvn

# ChildEBP RetAddr Args to Child

00 0006fe50 010015c4 0006fe6c 00000001 000851c8 badcgi!doDZ1+0xf (CONV: stdcall) [C:\build\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 429]

01 0006ff70 01001daf 00000001 00232468 00232cd8 badcgi!main+0x244 (CONV: cdecl) [C:\build\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 162]

02 0006ffc0 77e9ca90 000851c8 00000000 7ffdf000 badcgi!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]

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

 

 

 

 

Notice in frame 00 and 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.cpp:

 

Line 162 looks like

 

 

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

{

....

....

 

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

{

return doDZ1(rgchQuery, Variation); //Line 162

}

....

....

 

return (1);

} // main()

 

 

Line 429 looks like

 

DWORD doDZ1(CHAR szQS[DEFAULT_LEN], int Variation)

{

DWORD dwThreadID;

HANDLE hThread;

 

if (1 == Variation)

{

int i = 0;

int j = 1;

j = j / i;

return j; //line 429

}

 

if (2 == Variation)

{

HANDLE lpHandles[1];

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

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

}

 

return 1;

}

 

 

 

 

1) Integer Divide-By-Zero 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 Divide-ByZero Exception would look like:

 

 

0:001> kvn50

# ChildEBP RetAddr Args to Child

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

00 0049ffec 00000000 01001a26 00000000 00000000 badcgi+0x1a26

 

 

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 registers values

 

 

0:001> r

eax=00000000 ebx=00000000 ecx=00000101 edx=ffffffff esi=77e9186d edi=77fcbeda

eip=01001a26 esp=0049ffb8 ebp=0049ffec iopl=0 nv up ei pl zr na po nc

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

badcgi+1a26:

01001a26 6a01 push 0x1

 

 

c) The disassembly code

 

0:001> u eip

badcgi+1a26:

01001a26 6a01 push 0x1

01001a28 33c9 xor ecx,ecx

01001a2a 58 pop eax

01001a2b 99 cdq

01001a2c f7f9 idiv ecx

01001a2e c3 ret

01001a2f 55 push ebp

01001a30 8bec mov ebp,esp

 

 

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 Press 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 DBZ exception would look like:

 

 

0:001> kvn50

# ChildEBP RetAddr Args to Child

00 0049ffec 00000000 01001a26 00000000 00000000 badcgi!DBZ

 

Now, with the public symbols lined up, we see that the last call that caused the Divide-By-Zero Exception was to the function "DBZ()".

 

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"

 

First, we need to add the private symbol path(company wide symbols) as well as private 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 DBZ would look like:

 

 

 

0:001> kvn50

# ChildEBP RetAddr Args to Child

00 0049ffec 00000000 01001a26 00000000 00000000 badcgi!DBZ (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 514]

 

 

Notice in frame 00 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 514 looks like

 

 

DWORD DBZ()

{

int i = 0;

int j = 1;

j = j / i;

return j; //line 514

}

 

 


- Microsoft Confidential -