Debug Labs


 Heap Corruption/Old Pointer Reuse

This lab will demonstrate a heap corruption in a CGI Executable caused by reusing an already freed block. 

 

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
 
2- In the WinDbg command line, type in "g" and press "Enter". 

                 

 

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 reusing a block of memory already freed (Old Pointer Reuse).

 

 

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:

 

 

HEAP[badcgi.exe]: Number of free blocks in arena (2) does not match number in the free lists (3)

(8e4.b84): Break instruction exception - code 80000003 (first chance)

eax=00230000 ebx=00230050 ecx=0006f604 edx=0006f3ba esi=00000003 edi=00230000

eip=77f97704 esp=0006f588 ebp=0006f58c iopl=0 nv up ei pl nz na pe nc

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

ntdll!DbgBreakPoint:

77f97704 cc int 3

 

 

 

0:000> kvn

# ChildEBP RetAddr Args to Child

00 0006f584 77fb73c0 0006f5bc 77fb7163 00230000 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]

01 0006f58c 77fb7163 00230000 00230000 00230000 ntdll!RtlpBreakPointHeap+0x26 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 2374]

02 0006f5bc 77fad14f 00000208 00000040 00000003 ntdll!RtlpValidateHeap+0x33c (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 2332]

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

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

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

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

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

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

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

 

 

The 1st parameter to ntdll!RtlpValidateHeap ( 00000208 ) is a pointer to the _HEAP data structure. So this is what we are trying to validate.

Let dump the structure of _HEAP at that address.

 

 

0:000> dt _HEAP 0x00000208

+0x000 Entry : _HEAP_ENTRY

+0x008 Signature : ??

.......

.......

+0x580 Lookaside : ????

+0x584 LookasideLockCount : ??

Memory read error 0000078c

 

Well this confirms that the heap is corrupt.

 

The faulting stack in a heap corruption issue will involve heap operations. In this scenario, we are dealing with one thread

in the process "badCgi.exe". Once the HTTP request is processed, the process will get unloaded. So what happened here, is

The request was processed and while shutting down the process, the heap could not validated because it was corrupt, hence an AV.

 

 

Basically, we know in advance that the culprit is "badCgi.exe" because we are debugging it, but we still don't know what

function corrupted it!

 

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 part 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 "inetinfo.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:008> g

Page heap: block @ 06412FF0 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

(768.6ac): Break instruction exception - code 80000003 (first chance)

eax=00000000 ebx=00000027 ecx=77fb3d26 edx=049df182 esi=06412fd0 edi=00341000

eip=77f97704 esp=049df370 ebp=049df39c 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 0006fbe0 77fb3de6 000dcff0 77fb4248 000dcff0 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]

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

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

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

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

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

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

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

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

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 function call "doHC3" as well as the line of code in "badcgi.cpp"

 

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 323 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); //line 323

*ptr = 'x';

return (DWORD) ptr;

}

...

}

 

 

 

 

 


- Microsoft Confidential -