Debug Labs


Stack Overflow/Recursion in Component

This lab will demonstrate a stack overflow caused by a recursive function in the component.

 

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

 

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

 

2- Run Scenario

1- Click the "Invoke Component" button to load the COM Component in the dllhost process memory space. You should see the COM Component "badCom.dll"among the list of modules loaded in dllhost.exe
2- Click the "Cause Stack Overflow" button the first time to cause a 1st chance Stack Overflow 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 Stack Overflow happened
d- The assembly line that caused the Stack Overflow

					

                                                                           

 

1- Debug Annotation

 

1) Without symbols

 

a) The faulting Stack

 

The stack backtrace of the thread that caused the Stack Overflow would look like:

 

 

0:021> kvn

# ChildEBP RetAddr Args to Child

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

00 01e52ffc 02033131 000c0ea0 00000078 01e5301c badcom!DllUnregisterServer+0x13a6

01 01e5300c 02033131 000c0ea0 00000078 01e5302c badcom!DllUnregisterServer+0x13b4

02 01e5301c 02033131 000c0ea0 00000078 01e5303c badcom!DllUnregisterServer+0x13b4

03 01e5302c 02033131 000c0ea0 00000078 01e5304c badcom!DllUnregisterServer+0x13b4

04 01e5303c 02033131 000c0ea0 00000078 01e5305c badcom!DllUnregisterServer+0x13b4

05 01e5304c 02033131 000c0ea0 00000078 01e5306c badcom!DllUnregisterServer+0x13b4

06 01e5305c 02033131 000c0ea0 00000078 01e5307c badcom!DllUnregisterServer+0x13b4

07 01e5306c 02033131 000c0ea0 00000078 01e5308c badcom!DllUnregisterServer+0x13b4

08 01e5307c 02033131 000c0ea0 00000078 01e5309c badcom!DllUnregisterServer+0x13b4

09 01e5308c 02033131 000c0ea0 00000078 01e530ac badcom!DllUnregisterServer+0x13b4

0a 01e5309c 02033131 000c0ea0 00000078 01e530bc badcom!DllUnregisterServer+0x13b4

0b 01e530ac 02033131 000c0ea0 00000078 01e530cc badcom!DllUnregisterServer+0x13b4

0c 01e530bc 02033131 000c0ea0 00000078 01e530dc badcom!DllUnregisterServer+0x13b4

0d 01e530cc 02033131 000c0ea0 00000078 01e530ec badcom!DllUnregisterServer+0x13b4

0e 01e530dc 02033131 000c0ea0 00000078 01e530fc badcom!DllUnregisterServer+0x13b4

0f 01e530ec 02033131 000c0ea0 00000078 01e5310c badcom!DllUnregisterServer+0x13b4

10 01e530fc 02033131 000c0ea0 00000078 01e5311c badcom!DllUnregisterServer+0x13b4

11 01e5310c 02033131 000c0ea0 00000078 01e5312c badcom!DllUnregisterServer+0x13b4

12 01e5311c 02033131 000c0ea0 00000078 01e5313c badcom!DllUnregisterServer+0x13b4

13 01e5312c 02033131 000c0ea0 00000078 01e5314c badcom!DllUnregisterServer+0x13b4

 

It's obvious that in this stack backtrace, a function in badCom.dll (we don't know the function name yet because we don't have

have the symbols lined up yet) is calling itself without a satisfied stopping condition. This recursive call ended up by a stack

overflow which is basically an exception.

 

Note that there are other causes of a stack overflow other than recursive calls or infinite loops. Please refer to the windbg help. Search for "Stack Overflow"

 

0:021> .lastevent

Last event: 684.a18: Stack overflow - code c00000fd (!!! second chance !!!)

 

 

b) The regiters values

 

0:021> r

eax=000c0ea0 ebx=00000003 ecx=01e8f264 edx=000c0ea0 esi=00070000 edi=000c0ea0

eip=02033123 esp=01e53000 ebp=01e5300c iopl=0 nv up ei pl zr na po nc

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

badcom!DllUnregisterServer+13a6:

02033123 55 push ebp

 

 

c) The disassmbly code

 

0:021> u eip

badcom!DllUnregisterServer+13a6:

02033123 55 push ebp

02033124 8bec mov ebp,esp

02033126 ff750c push dword ptr [ebp+0xc]

02033129 ff7508 push dword ptr [ebp+0x8]

0203312c e8f2ffffff call badcom!DllUnregisterServer+0x13a6 (02033123)

02033131 5d pop ebp

02033132 c20800 ret 0x8

02033135 55 push ebp

 

 

2) With symbols including public symbols of "badCom.dll"

 

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

 

0:021> kvn

# ChildEBP RetAddr Args to Child

00 01e52ffc 02033131 000c0ea0 00000078 01e5301c badcom!FindChar

01 01e5300c 02033131 000c0ea0 00000078 01e5302c badcom!FindChar+0xe

02 01e5301c 02033131 000c0ea0 00000078 01e5303c badcom!FindChar+0xe

03 01e5302c 02033131 000c0ea0 00000078 01e5304c badcom!FindChar+0xe

04 01e5303c 02033131 000c0ea0 00000078 01e5305c badcom!FindChar+0xe

05 01e5304c 02033131 000c0ea0 00000078 01e5306c badcom!FindChar+0xe

06 01e5305c 02033131 000c0ea0 00000078 01e5307c badcom!FindChar+0xe

07 01e5306c 02033131 000c0ea0 00000078 01e5308c badcom!FindChar+0xe

08 01e5307c 02033131 000c0ea0 00000078 01e5309c badcom!FindChar+0xe

09 01e5308c 02033131 000c0ea0 00000078 01e530ac badcom!FindChar+0xe

0a 01e5309c 02033131 000c0ea0 00000078 01e530bc badcom!FindChar+0xe

0b 01e530ac 02033131 000c0ea0 00000078 01e530cc badcom!FindChar+0xe

0c 01e530bc 02033131 000c0ea0 00000078 01e530dc badcom!FindChar+0xe

0d 01e530cc 02033131 000c0ea0 00000078 01e530ec badcom!FindChar+0xe

0e 01e530dc 02033131 000c0ea0 00000078 01e530fc badcom!FindChar+0xe

0f 01e530ec 02033131 000c0ea0 00000078 01e5310c badcom!FindChar+0xe

10 01e530fc 02033131 000c0ea0 00000078 01e5311c badcom!FindChar+0xe

11 01e5310c 02033131 000c0ea0 00000078 01e5312c badcom!FindChar+0xe

12 01e5311c 02033131 000c0ea0 00000078 01e5313c badcom!FindChar+0xe

13 01e5312c 02033131 000c0ea0 00000078 01e5314c badcom!FindChar+0xe

 

So basically here, we can confirm that the call to the function "FindChar()" is recursive without a satisfied stopping condition. This enough to tell the customer

that the problem is coming from the function "FindChar()" in the custom COM Component "badCom.dll"

   

Advanced Technique

 

In IIS4 and 5, each thread has a stack of a maximum size of 256k. Please refer to teh following KB:

PRB: Stack Size Now 256 K in Internet Information Server 4.0 WGID:246

ID: 189888.KB.EN-US CREATED: 1998-07-21 MODIFIED: 2001-07-16

 

We will be using the !teb command (Thread Environment Variable) to check the stack size.

 

 

 

0:021> !teb

TEB at 7FFA4000

ExceptionList: 1e8f264

Stack Base: 1e90000

Stack Limit: 1e51000

SubSystemTib: 0

FiberData: 1e00

ArbitraryUser: 0

Self: 7ffa4000

EnvironmentPtr: 0

ClientId: ac0.700

Real ClientId: ac0.700

RpcHandle: 0 Tls

Storage: 0 PEB

Address: 7ffdf000

LastErrorValue: 0

LastStatusValue: c0000034

Count Owned Locks:0

HardErrorsMode: 0

 

   

Finding the current size:

 

0:021> ? 1e90000-1e51000

Evaluate expression: 258048 = 0003f000

 

0:021> ?258048 /1024

Evaluate expression: 594 = 00000252

 

 

This shows that the current size now is 252 KB which is almost the limit

 

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

 

 

0:021> lmv mbadcom

start end module name

02030000 02038000 badcom (pdb symbols) c:\iisdebuglabs\symbols\pub\badcom.pdb

Loaded symbol image file: badcom.dll

Image path: c:\IISDEB~1\www\app\badcom.dll

Timestamp: Tue Jun 10 22:45:51 2003 (3EE697DF) Checksum: 0000F229

File version: 1.0.0.1

Product version: 1.0.0.1

File flags: 0 (Mask 3F)

File OS: 4 Unknown Win32

File type: 2.0 Dll

File date: 00000000.00000000

CompanyName:

ProductName: badCOM

Module InternalName: badCOM

OriginalFilename: badCOM.DLL

ProductVersion: 1, 0, 0, 1

FileVersion: 1, 0, 0, 1

FileDescription: badCOM Module

LegalCopyright: Copyright 2003

 

 

3) with Private Symbols "badExt.dll"

 

 

First, we need to add the private 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

 

The stack backtrace of the thread that encountered the stack corruption would look like:

 

 

0:021> kvn

# ChildEBP RetAddr Args to Child

00 01e52ffc 02033131 000c0ea0 00000078 01e5301c badcom!FindChar (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 575]

01 01e5300c 02033131 000c0ea0 00000078 01e5302c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

02 01e5301c 02033131 000c0ea0 00000078 01e5303c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

03 01e5302c 02033131 000c0ea0 00000078 01e5304c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

04 01e5303c 02033131 000c0ea0 00000078 01e5305c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

05 01e5304c 02033131 000c0ea0 00000078 01e5306c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

06 01e5305c 02033131 000c0ea0 00000078 01e5307c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

07 01e5306c 02033131 000c0ea0 00000078 01e5308c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

08 01e5307c 02033131 000c0ea0 00000078 01e5309c badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

09 01e5308c 02033131 000c0ea0 00000078 01e530ac badcom!FindChar+0xe (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 585]

 

......

......

 

 

Looking at the source code:

 

char* FindChar(char* sz, char c)

{

char* ch = NULL;

{

ch = sz++;

sz = FindChar( ch, c ); //Line 347

}

return sz;

}

As you see this recursive function will never stop execution becuase there is no stopping condition. In real life

this kind of problems would happen if the stopping condition is never met for some reason. 


- Microsoft Confidential -