SafeArrayGetVartype


Author: David Zimmer
Date: 09.12.19 - 8:22pm



Tonight's tip comes from Mark Johnstone who was doing research into how safe arrays can store type information. The safe array structure itself does not have an internal type field.

In my vbdec data viewer form I was giving the user the choice on how to decode the data based on element size, essentially brute forcing it and going on intution base on if displayed values made sense, Not the best approach! lol

Mark found an undocumented pre-structure data blob that can contain type information.
Awesome find and thanks for sharing the hard fought research Mark!

Its pretty safe to say I would have never stumbled across this. (Had never even heard of SafeArrayGetVartype yet)

Here is Marks code: ManagedCharSafeArray.cls along with some links that turned up in the course of his research: ReactOS Safearray Source, and Where is SAFEARRAY var type stored?.

I will mirror the key bits of the stack overflow info here:

Where is SAFEARRAY var type stored?

I'd like to know about SAFEARRAY implementation.

It seems to me that there's no field in SAFEARRAY structure
 that is used for storing element type information, such as
 VT_I4(3) or VT_R4(4), but SafeArrayGetVartype function 
returns the correct type.

Somebody commented on the MSDN page below saying that the 
high word of the cLocks holds the type info: SAFEARRAY 
structure on MSDN

But when I passed Long and Single arrays from VBA to a DLL 
function via a type libray, those arrays' fFeatures are
 both 0x80, cLocks are both 0, and stll SafeArrayGetVartype 
can tell VT_I4(3) and VT_R4(4).

asked Sep 13 '13 at 10:49 - Fumito Hamamura

--------------------------------

Depending on how the safearray was created, the variant type
 may be stored in memory before (at the offset -4 from the 
start of) the SAFEARRAY structure. FADF_HAVEVARTYPE flag 
in fFeatures indicates whether the type is available.

Similarly, FADF_HAVEIID indicates that the GUID (see 
SafeArrayCreateEx) is stored at offset of -16, and 
available via SafeArrayGetIID. FADF_HAVEVARTYPE and 
FADF_HAVEIID can never be present simultaneously 
(because otherwise the VARTYPE and the GUID would overlap
 in memory), but SafeArrayGetVartype is smart enough to 
synthesize one of VT_RECORD, VT_DISPATCH or VT_UNKNOWN 
types when it sees corresponding feature flags.

answered Sep 13 '13 at 12:55 - Igor Tandetnik

--------------------------------------

Thank you for the clear answer. So, to summarize, for 
the fFeatures field, 
A) FADF_HAVEVARTYPE or 
B) FADF_HAVEIID or 
C) FADF_RECORD can be set. 

For A) 4, for B) 16, for C) 4 bytes before SAFEARRAY are 
used to carry supplemental information about the SAFEARRAY. 

Only one of the A, B or C can be set at a time. – 

Fumito Hamamura Sep 13 '13 at 14:08






Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 25 + 24 = ? followed by the letter: F 



Twitter
RSS
About Me
More Blogs
Main Site
Posts: (All)
2020 ( 5 )
2019 (12)
     Yara WorkBench
     SafeArrayGetVartype
     vbdec dbg updates
     vb6 PCode NOP
     vb6 API and call backs
     how pcode works Pt1
     PrintFile
     ImpAdCallNonVirt
     Reversing PCode Args
     VB6 PCode Disassembly
     VB6 PCode Debugger
     UConnect Disable Cell Modem
2017 (5)
     IDA python over IPC
     dns wildcard blocking
     64bit IDA Plugins
     anterior lines
     misc news/updates
2016 ( 4 )
2015 ( 6 )
2014 ( 5 )
2013 ( 9 )
2012 ( 13 )
2011 ( 19 )
2010 ( 11 )
2009 ( 1 )