From 545c3019cbb6136fe0b6ca5154b685b64545c6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Sat, 13 Apr 2019 19:23:59 +0200 Subject: [PATCH 1/2] Bind Wbemcli.IWbemClassObject#GetNames --- .../sun/jna/platform/win32/COM/Wbemcli.java | 41 +++++++++++++++++++ .../jna/platform/win32/COM/WbemcliTest.java | 9 ++++ 2 files changed, 50 insertions(+) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java b/contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java index f0d62b4989..1fccfc8d37 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java @@ -27,6 +27,8 @@ import com.sun.jna.WString; import com.sun.jna.platform.win32.Guid.CLSID; import com.sun.jna.platform.win32.Guid.GUID; +import com.sun.jna.platform.win32.OaIdl.SAFEARRAY; +import com.sun.jna.platform.win32.OaIdlUtil; import com.sun.jna.platform.win32.Ole32; import com.sun.jna.platform.win32.OleAuto; import com.sun.jna.platform.win32.Variant.VARIANT; @@ -81,6 +83,24 @@ public interface Wbemcli { public static final int CIM_OBJECT = 13; public static final int CIM_FLAG_ARRAY = 0x2000; + public interface WBEM_CONDITION_FLAG_TYPE { + public static final int WBEM_FLAG_ALWAYS = 0; + public static final int WBEM_FLAG_ONLY_IF_TRUE = 0x1; + public static final int WBEM_FLAG_ONLY_IF_FALSE = 0x2; + public static final int WBEM_FLAG_ONLY_IF_IDENTICAL = 0x3; + public static final int WBEM_MASK_PRIMARY_CONDITION = 0x3; + public static final int WBEM_FLAG_KEYS_ONLY = 0x4; + public static final int WBEM_FLAG_REFS_ONLY = 0x8; + public static final int WBEM_FLAG_LOCAL_ONLY = 0x10; + public static final int WBEM_FLAG_PROPAGATED_ONLY = 0x20; + public static final int WBEM_FLAG_SYSTEM_ONLY = 0x30; + public static final int WBEM_FLAG_NONSYSTEM_ONLY = 0x40; + public static final int WBEM_MASK_CONDITION_ORIGIN = 0x70; + public static final int WBEM_FLAG_CLASS_OVERRIDES_ONLY = 0x100; + public static final int WBEM_FLAG_CLASS_LOCAL_AND_OVERRIDES = 0x200; + public static final int WBEM_MASK_CLASS_CONDITION = 0x300; + } + /** * Contains and manipulates both WMI class definitions and class object * instances. @@ -105,6 +125,27 @@ public HRESULT Get(String wszName, int lFlags, VARIANT.ByReference pVal, IntByRe IntByReference plFlavor) { return Get(wszName == null ? null : new WString(wszName), lFlags, pVal, pType, plFlavor); } + + public HRESULT GetNames(String wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal, PointerByReference pNames) { + return GetNames(wszQualifierName == null ? null : new WString(wszQualifierName), lFlags, pQualifierVal, pNames); + } + + public HRESULT GetNames(WString wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal, PointerByReference pNames) { + // 8th method in IWbemClassObjectVtbl + return (HRESULT) _invokeNativeObject(7, + new Object[]{ getPointer(), wszQualifierName, lFlags, pQualifierVal, pNames}, HRESULT.class); + } + + public String[] GetNames(String wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal) { + PointerByReference pbr = new PointerByReference(); + COMUtils.checkRC(GetNames(wszQualifierName, lFlags, pQualifierVal, pbr)); + Object[] nameObjects = (Object[]) OaIdlUtil.toPrimitiveArray(new SAFEARRAY(pbr.getValue()), true); + String[] names = new String[nameObjects.length]; + for(int i = 0; i < nameObjects.length; i++) { + names[i] = (String) nameObjects[i]; + } + return names; + } } /** diff --git a/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java b/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java index b79db37af2..763ba265f7 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java @@ -44,6 +44,9 @@ import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult; import com.sun.jna.platform.win32.OleAuto; import com.sun.jna.ptr.IntByReference; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; /** * Test class for Wbemcli and WbemcliUti methods and classes used to query WMI. @@ -189,6 +192,12 @@ public void testShowProperties() { for(IWbemClassObject iwco: results) { resultCount++; + Set names = new HashSet(Arrays.asList(iwco.GetNames(null, 0, null))); + assertTrue(names.contains("CommandLine")); + assertTrue(names.contains("ProcessId")); + assertTrue(names.contains("WorkingSetSize")); + assertTrue(names.contains("ExecutionState")); + assertTrue(names.contains("CreationDate")); try { // COMMANDLINE is STRING = VT_BSTR iwco.Get("COMMANDLINE", 0, pVal, pType, plFlavor); From 126af4ecf2ebe1347f664563883289b7a4a47ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Sun, 14 Apr 2019 14:59:53 +0200 Subject: [PATCH 2/2] Prevent access to unsupported values in `c.s.j.p.win32.COM.WbemcliUtil#enumerateProperties`1 --- CHANGES.md | 1 + .../jna/platform/win32/COM/WbemcliUtil.java | 18 ++++++++++++++++-- .../jna/platform/win32/COM/WbemcliTest.java | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 40cd0916ca..c0e4ae2887 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Features * [#1068](https://github.com/java-native-access/jna/pull/1068): `c.s.j.p.win32.Advapi32Util.getAccountBySid(String systemName, PSID sid)` ignored parameter instead of passing it to the native function - [@nirud](https://github.com/nirud). * [#813](https://github.com/java-native-access/jna/issues/813): Support for default methods on interfaces (experimental) - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#1073](https://github.com/java-native-access/jna/issues/1073): Support COM setters with multiple parameters using `c.s.j.p.win32.COM.util.ProxyObject` - [@matthiasblaesing](https://github.com/matthiasblaesing). +* [#1083](https://github.com/java-native-access/jna/issues/1083): Prevent access to unsupported values in `c.s.j.p.win32.COM.WbemcliUtil#enumerateProperties` and bind `c.s.j.p.win32.COM.Wbemcli.IWbemClassObject.GetNames` - [@matthiasblaesing](https://github.com/matthiasblaesing). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java b/contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java index 3589f9f0ae..828361b7d3 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java @@ -41,6 +41,10 @@ import com.sun.jna.platform.win32.COM.Wbemcli.IWbemClassObject; import com.sun.jna.platform.win32.COM.Wbemcli.IWbemLocator; import com.sun.jna.platform.win32.COM.Wbemcli.IWbemServices; +import static com.sun.jna.platform.win32.Variant.VT_ARRAY; +import static com.sun.jna.platform.win32.Variant.VT_DISPATCH; +import static com.sun.jna.platform.win32.Variant.VT_UNKNOWN; +import static com.sun.jna.platform.win32.Variant.VT_VECTOR; import com.sun.jna.ptr.IntByReference; /** @@ -271,7 +275,9 @@ private static > IEnumWbemClassObject selectProperties(IWbemSe * will always wait for results. * * @return A WmiResult object encapsulating an EnumMap which will hold - * the results. + * the results. Values, that are not supported by this helper + * ({@code Dispatch}, {@code Unknown}, {@code SAFEARRAY}) are + * not returned and reported as {@code null}. * * @throws TimeoutException * if the query times out before completion @@ -336,12 +342,20 @@ private static > WmiResult enumerateProperties(IEnumWbemCla case Variant.VT_R8: values.add(vtType, cimType, property, pVal.doubleValue()); break; + case Variant.VT_EMPTY: case Variant.VT_NULL: values.add(vtType, cimType, property, null); break; // Unimplemented type. User must cast default: - values.add(vtType, cimType, property, pVal.getValue()); + if(((vtType & VT_ARRAY) == VT_ARRAY) || + ((vtType & VT_UNKNOWN) == VT_UNKNOWN)|| + ((vtType & VT_DISPATCH) == VT_DISPATCH)|| + ((vtType & VT_VECTOR) == VT_VECTOR)) { + values.add(vtType, cimType, property, null); + } else { + values.add(vtType, cimType, property, pVal.getValue()); + } } OleAuto.INSTANCE.VariantClear(pVal); } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java b/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java index 763ba265f7..9d6ae26352 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java @@ -291,4 +291,22 @@ public void testWmiOperatingSystem() { assertEquals(Variant.VT_BOOL, os.getVtType(OperatingSystemProperty.PRIMARY)); assertNotNull(os.getValue(OperatingSystemProperty.PRIMARY, 0)); } + + enum Win32_DiskDrive_Values { + CAPTION, + CAPABILITIES + } + + @Test + public void testUnsupportedValues() { + WmiQuery serialNumberQuery = new WmiQuery("Win32_DiskDrive", Win32_DiskDrive_Values.class); + WmiResult result = serialNumberQuery.execute(); + assertTrue(result.getResultCount() > 0); + for (int i = 0; i < result.getResultCount(); i++) { + assertNotNull(result.getValue(Win32_DiskDrive_Values.CAPTION, i)); + // Capabilities are represented by a SAFEARRAY, this not supported + // in the simplified API + assertNull(result.getValue(Win32_DiskDrive_Values.CAPABILITIES, i)); + } + } }