Debug Labs


Heap Corruption/Double Free

This lab will demonstrate a heap corruption in a CGI Executable caused by a double free. 

 

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 "Corrupt Heap" button. 
 Note that the debugger breaks and diplays 
	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
Important Note: If the debugger does not break, Issue a "Ctrl-Break". Debugger will then wait for 30s to break.

                 

 

1- Debug Annotation

Definition:

 

Heap Corruption occurs when a thread allocates a block of heap memory of a given size and then writes to memory

addresses beyond the requested size of the heap block. Another common cause of heap corruption is writing to block

of memory that has been freed or freeing a block of memory already freed.

 

In this scenario, we will try to demonstrate heap corruption caused by freeing a block of memory already freed (Double Free).

 

 

1) Without Page Heap Enabled.

 

The faulting Stack

 

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

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:

0:008> .sympath+ SRV*C:\LabSymbols*\\symbols\symbols

Symbol search path is: SRV*C:\LabSymbols*\\symbols\symbols

 

0:008> .sympath+ C :\IISDebugLabs\symbols\Pri (where private symbols of badFil.dll are located)

Symbol search path is:SRV*C:\LabSymbols* \\symbols\symbols; C :\IISDebugLabs\symbols\Pub

 

Note: For more information on setting the symbol path for windbg, please refer to the debugger documentation for the .sympath command. This will describe

the "downstream store" feature that lets you download symbols to a symbol server of your own.

 

The stack backtrace of the thread that AV'd would look like:

 

In this scenario, the debugger did not break because the process was in a hang state.

Here how the call stacks look like:

 

. 0 Id: 840.9d0 Suspend: 1 Teb: 7ffde000 Unfrozen

ChildEBP RetAddr Args to Child 0006f5c0 77f93736 00230718 00000003 00000002 ntdll!RtlpAllocateFromHeapLookaside+0x59 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\lookasid.c @ 272]

0006f614 77fad222 00230000 00000000 00000000 ntdll!RtlValidateHeap+0x83 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdll.c @ 2603]

0006fe40 77f9b001 000726e8 77f896c3 7ffdf000 ntdll!RtlValidateProcessHeaps+0x79 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdll.c @ 2767]

0006fe98 77e9cfb1 77f82b95 00000001 7ffdf000 ntdll!LdrShutdownProcess+0xbf (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\dll\ldrinit.c @ 1543]

0006ff5c 7800c347 000726e8 77f83920 7800c2af KERNEL32!ExitProcess+0x51 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\process.c @ 2881]

0006ff78 01001e7c 000726e8 77f82b95 77f83920 MSVCRT!doexit+0xb0 (FPO: [3,0,2]) (CONV: cdecl) [crt0dat.c @ 394]

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

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

 

 

1 Id: 840.9a8 Suspend: 1 Teb: 7ffdd000 Unfrozen

ChildEBP RetAddr Args to Child 0028fc24 77f85c55 00000044 00000000 00000000 ntdll!ZwWaitForSingleObject+0xb (FPO: [3,0,0]) [i386\usrstubs.asm @ 2004]

0028fc98 77f85bd1 77fcf301 77f90300 77fcf348 ntdll!RtlpWaitForCriticalSection+0x9e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\dll\resource.c @ 1612]

0028fca0 77f90300 77fcf348 0028fd30 00000000 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0]) [..\i386\critsect.asm @ 157]

0028fd1c 77fa02e3 0028fd30 77f80000 00000000 ntdll!LdrpInitialize+0x1a7 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\dll\ldrinit.c @ 456]

00000000 00000000 00000000 00000000 00000000 ntdll!KiUserApcDispatcher+0x7 [..\i386\userdisp.asm @ 103]

 

 

It looks like thread 0 is trying to exit the process and it's validating the heaps. It owns the loader lock critical section that it does not seem to release.

So the process will be hung.

 

The reason this is happening is because while validating the heaps, the thread came across a corrupt lookaside list.

 

 

0:000> dt _HEAP_LOOKASIDE 0x00230718

+0x000 ListHead : _SLIST_HEADER

+0x008 Depth : 4

+0x00a MaximumDepth : 0x100

+0x00c TotalAllocates : 0x329410b

+0x010 AllocateMisses : 8

+0x014 TotalFrees : 2

+0x018 FreeMisses : 0

+0x01c LastTotalAllocates : 0

+0x020 LastAllocateMisses : 0

+0x024 Future : [2] 0

 

 

This shows the heap lookaside data structure at 0x00230718 . this address should be a valid head of the lookaside linked list.

It seems that it is not a valid header, hence corruption.

 

   

To know who corrupted the heap, we need to Enable Page Heap (A memory management feature to track more closely all heap operations).

This second of this debug annotation shows how to do that and identify the culprit.

 

2) With Page Heap Enabled.

 

a) Turn On "Enable Page Heap"

 

- Go to the Command Prompt

- Switch to the folder where the debugging tools for windows is installed

- Type in "GFlags" and hit Enter

- In the "Image File Name", Enter "badcgi.exe" without quotes

- Click "Image File Options" radio button

- Click "Enable Page Heap" check box

- Click "Apply"

- Click "OK"

- Restart IIS

 

b) Run the scenario again

 

- Do Step 3 and 4 in the "Lab Setup" Section

- Click "Corrupt Heap" one time

	- Note that the debugger breaks and diplays 
		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
       - In the WinDbg command line, type in "g" and press "Enter". 
            - Notice the "Page Heap" Debug output. 
                   
	c) Line up the Private Symbols                    

 

- Point the debugger to use the private symbols

- For company wide internal symbols, use \\symbols\symbols

- For the component private symbols, use C:\IISDebugLabs\Symbols\Pri

 

 

The faulting Stack

 

 

0:000> g

Page heap: block @ 000DCFF0 is corrupted (reason 27)

Page heap: reason: corrupted start stamp

Page heap: reason: corrupted end stamp

Page heap: reason: corrupted heap pointer

Page heap: reason: raised exception while probing

( 868.2a8): Break instruction exception - code 80000003 (first chance)

eax=00000000 ebx=00000027 ecx=77fb3d26 edx=0006f9fa esi=000dcfd0 edi= 00341000

eip=77f97704 esp=0006fbe8 ebp=0006fc14 iopl=0 nv up ei pl zr na po nc

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

ntdll!DbgBreakPoint:

77f97704 cc int 3

 

 

 

This shows the debug output of "Page Heap" showing the attempt of corrupting the heap by the thread shown blow.

 

 

The stack backtrace of the thread that AV'd would look like:

 

0:000> kvn

# ChildEBP RetAddr Args to Child 00 0006fbe4 77fb3de6 000dcff0 77fb4248 000dcff0 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]

01 0006fbec 77fb4248 000dcff0 00000027 00341000 ntdll!RtlpDphReportCorruptedBlock+0x8c (FPO: [2,0,1]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4142]

02 0006fc14 77fb2f3f 00341000 01001002 000dcff0 ntdll!RtlpDphNormalHeapFree+0x46 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 4575]

03 0006fc38 77fb5887 00340000 01001002 000dcff0 ntdll!RtlpDebugPageHeapFree+0xa6 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heappage.c @ 2862]

04 0006fcb0 77fa7111 00340000 01001002 000dcff0 ntdll!RtlDebugFreeHeap+0x203 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 915]

05 0006fd50 77fcbeda 00340000 01001002 000dcff0 ntdll!RtlFreeHeapSlowly+0x4d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3466]

06 0006fdf4 7800115c 00340000 00000000 000dcff0 ntdll!RtlFreeHeap+0x53 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heap.c @ 3138]

07 0006fe3c 01001766 000dcff0 003b8ff0 78002041 MSVCRT!free+0xda (CONV: cdecl) [free.c @ 101]

08 0006ff70 01001e6f 00000001 003b6fd0 00350f30 badcgi!doHC2+0x2f (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 285]

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

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

 

 

 

So basically, this is the thread that corrupted the heap, It's shows now that the culprit is "badCgi.exe".

Notice also the function call "doHC2" as well as the line of code in "baCgi.exe"

 

Verify the filter's symbols are lined up.

 

Verify the filter's symbols are lined up.

 

 

0:000> lmv mbadcgi

start end module name

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

Loaded symbol image file: badcgi.exe Image path: C:\IISDebugLabs\www\app\badcgi.exe

Timestamp: Sun Jun 22 20:08:49 2003 (3EF64511) Checksum: 00002AC2

 

   

 

Line 285 looks like

 

DWORD doHC2(int Variation)//Double Free

{

if (1 == Variation)

{

char *ptr, *tmp;

int i;

ptr = (char*)GlobalAlloc(GMEM_FIXED, 16);

tmp = ptr;

for (i = 0; i < 16; ++i)

*(tmp++) = 'b';

free(ptr);

free(tmp);

free (ptr); //line 285

return (DWORD) ptr;

}

...

}

 

 

 

 

 


- Microsoft Confidential -