1 /*
   2  * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "runtime/os.hpp"
  27 #include "runtime/vm_version.hpp"
  28 
  29 #include <comdef.h>
  30 #include <Wbemidl.h>
  31 
  32 #pragma comment(lib, "wbemuuid.lib")
  33 
  34 #define VM_MODEL L"Virtual Machine"
  35 
  36 
  37 bool VM_Version::is_in_VM() {
  38   HRESULT hres;
  39 
  40   hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  41   if (hres != S_OK) {
  42     return false;
  43   }
  44 
  45   hres = CoInitializeSecurity(NULL, -1, NULL, NULL,
  46                               RPC_C_AUTHN_LEVEL_DEFAULT,
  47                               RPC_C_IMP_LEVEL_IMPERSONATE,
  48                               NULL, EOAC_NONE, NULL);
  49   if (hres != S_OK) {
  50     CoUninitialize();
  51     return false;
  52   }
  53 
  54   IWbemLocator* pLoc = NULL;
  55   hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
  56                           IID_IWbemLocator, (LPVOID*)&pLoc);
  57   if (hres != S_OK) {
  58     CoUninitialize();
  59     return false;
  60   }
  61 
  62   IWbemServices* pSvc = NULL;
  63   BSTR resource = SysAllocString(L"ROOT\\CIMV2");
  64   hres = pLoc->ConnectServer(resource,
  65                             NULL, NULL, 0, NULL, 0, 0, &pSvc);
  66   SysFreeString(resource);
  67   if (hres != S_OK) {
  68     pLoc->Release();
  69     CoUninitialize();
  70     return false;
  71   }
  72 
  73   hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
  74                            NULL, RPC_C_AUTHN_LEVEL_CALL,
  75                            RPC_C_IMP_LEVEL_IMPERSONATE,
  76                            NULL, EOAC_NONE);
  77   if (hres != S_OK) {
  78     pSvc->Release();
  79     pLoc->Release();
  80     CoUninitialize();
  81     return false;
  82   }
  83 
  84   IEnumWbemClassObject* pEnumerator = NULL;
  85   BSTR lang = SysAllocString(L"WQL");
  86   BSTR query = SysAllocString(L"SELECT Model FROM Win32_ComputerSystem");
  87   hres = pSvc->ExecQuery(lang, query,
  88                          WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  89                          NULL, &pEnumerator);
  90   SysFreeString(lang);
  91   SysFreeString(query);
  92   if (hres != S_OK) {
  93     pSvc->Release();
  94     pLoc->Release();
  95     CoUninitialize();
  96     return false;
  97   }
  98 
  99   bool result = false;
 100   IWbemClassObject* pclsObj = NULL;
 101   ULONG uReturn = 0;
 102   hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
 103   if (uReturn != 0) {
 104     VARIANT vtProp;
 105     hres = pclsObj->Get(L"Model", 0, &vtProp, 0, 0);
 106     if (hres == S_OK) {
 107       if (wcscmp(VM_MODEL, vtProp.bstrVal) == 0) {
 108         result = true;
 109       }
 110       VariantClear(&vtProp);
 111     }
 112     pclsObj->Release();
 113   }
 114 
 115   pSvc->Release();
 116   pLoc->Release();
 117   pEnumerator->Release();
 118   CoUninitialize();
 119 
 120   return result;
 121 }