| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome_elf/chrome_elf_util.h" | |
| 6 | |
| 7 #include <assert.h> | |
| 8 #include <windows.h> | |
| 9 #include <stddef.h> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/strings/string16.h" | |
| 13 | |
| 14 ProcessType g_process_type = ProcessType::UNINITIALIZED; | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; | |
| 19 const wchar_t kRegPathClientStateMedium[] = | |
| 20 L"Software\\Google\\Update\\ClientStateMedium"; | |
| 21 #if defined(GOOGLE_CHROME_BUILD) | |
| 22 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | |
| 23 #else | |
| 24 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium"; | |
| 25 #endif // defined(GOOGLE_CHROME_BUILD) | |
| 26 | |
| 27 const wchar_t kRegValueUsageStats[] = L"usagestats"; | |
| 28 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; | |
| 29 const wchar_t kMetricsReportingEnabled[] =L"MetricsReportingEnabled"; | |
| 30 | |
| 31 const wchar_t kAppGuidCanary[] = | |
| 32 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; | |
| 33 const wchar_t kAppGuidGoogleChrome[] = | |
| 34 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
| 35 const wchar_t kAppGuidGoogleBinaries[] = | |
| 36 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; | |
| 37 | |
| 38 bool ReadKeyValueString(bool system_install, const wchar_t* key_path, | |
| 39 const wchar_t* guid, const wchar_t* value_to_read, | |
| 40 base::string16* value_out) { | |
| 41 HKEY key = NULL; | |
| 42 value_out->clear(); | |
| 43 | |
| 44 base::string16 full_key_path(key_path); | |
| 45 full_key_path.append(1, L'\\'); | |
| 46 full_key_path.append(guid); | |
| 47 | |
| 48 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, | |
| 49 full_key_path.c_str(), 0, | |
| 50 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) != | |
| 51 ERROR_SUCCESS) { | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 const size_t kMaxStringLength = 1024; | |
| 56 wchar_t raw_value[kMaxStringLength] = {}; | |
| 57 DWORD size = sizeof(raw_value); | |
| 58 DWORD type = REG_SZ; | |
| 59 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type, | |
| 60 reinterpret_cast<LPBYTE>(raw_value), &size); | |
| 61 | |
| 62 if (result == ERROR_SUCCESS) { | |
| 63 if (type != REG_SZ || (size & 1) != 0) { | |
| 64 result = ERROR_NOT_SUPPORTED; | |
| 65 } else if (size == 0) { | |
| 66 *raw_value = L'\0'; | |
| 67 } else if (raw_value[size / sizeof(wchar_t) - 1] != L'\0') { | |
| 68 if ((size / sizeof(wchar_t)) < kMaxStringLength) | |
| 69 raw_value[size / sizeof(wchar_t)] = L'\0'; | |
| 70 else | |
| 71 result = ERROR_MORE_DATA; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 if (result == ERROR_SUCCESS) | |
| 76 *value_out = raw_value; | |
| 77 | |
| 78 ::RegCloseKey(key); | |
| 79 | |
| 80 return result == ERROR_SUCCESS; | |
| 81 } | |
| 82 | |
| 83 bool ReadKeyValueDW(bool system_install, const wchar_t* key_path, | |
| 84 base::string16 guid, const wchar_t* value_to_read, | |
| 85 DWORD* value_out) { | |
| 86 HKEY key = NULL; | |
| 87 *value_out = 0; | |
| 88 | |
| 89 base::string16 full_key_path(key_path); | |
| 90 full_key_path.append(1, L'\\'); | |
| 91 full_key_path.append(guid); | |
| 92 | |
| 93 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, | |
| 94 full_key_path.c_str(), 0, | |
| 95 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) != | |
| 96 ERROR_SUCCESS) { | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 DWORD size = sizeof(*value_out); | |
| 101 DWORD type = REG_DWORD; | |
| 102 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type, | |
| 103 reinterpret_cast<BYTE*>(value_out), &size); | |
| 104 | |
| 105 ::RegCloseKey(key); | |
| 106 | |
| 107 return result == ERROR_SUCCESS && size == sizeof(*value_out); | |
| 108 } | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 112 bool IsCanary(const wchar_t* exe_path) { | |
| 113 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; | |
| 114 } | |
| 115 | |
| 116 bool IsSystemInstall(const wchar_t* exe_path) { | |
| 117 wchar_t program_dir[MAX_PATH] = {}; | |
| 118 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, | |
| 119 arraysize(program_dir)); | |
| 120 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) | |
| 121 return true; | |
| 122 | |
| 123 ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir, | |
| 124 arraysize(program_dir)); | |
| 125 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) | |
| 126 return true; | |
| 127 | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 bool IsMultiInstall(bool is_system_install) { | |
| 132 base::string16 args; | |
| 133 if (!ReadKeyValueString(is_system_install, kRegPathClientState, | |
| 134 kAppGuidGoogleChrome, kUninstallArgumentsField, | |
| 135 &args)) { | |
| 136 return false; | |
| 137 } | |
| 138 return args.find(L"--multi-install") != base::string16::npos; | |
| 139 } | |
| 140 | |
| 141 bool AreUsageStatsEnabled(const wchar_t* exe_path) { | |
| 142 bool enabled = true; | |
| 143 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); | |
| 144 | |
| 145 if (controlled_by_policy && !enabled) | |
| 146 return false; | |
| 147 | |
| 148 bool system_install = IsSystemInstall(exe_path); | |
| 149 base::string16 app_guid; | |
| 150 | |
| 151 if (IsCanary(exe_path)) { | |
| 152 app_guid = kAppGuidCanary; | |
| 153 } else { | |
| 154 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries : | |
| 155 kAppGuidGoogleChrome; | |
| 156 } | |
| 157 | |
| 158 DWORD out_value = 0; | |
| 159 if (system_install && | |
| 160 ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid, | |
| 161 kRegValueUsageStats, &out_value)) { | |
| 162 return out_value == 1; | |
| 163 } | |
| 164 | |
| 165 return ReadKeyValueDW(system_install, kRegPathClientState, app_guid, | |
| 166 kRegValueUsageStats, &out_value) && out_value == 1; | |
| 167 } | |
| 168 | |
| 169 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { | |
| 170 HKEY key = NULL; | |
| 171 DWORD value = 0; | |
| 172 BYTE* value_bytes = reinterpret_cast<BYTE*>(&value); | |
| 173 DWORD size = sizeof(value); | |
| 174 DWORD type = REG_DWORD; | |
| 175 | |
| 176 if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0, | |
| 177 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { | |
| 178 if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type, | |
| 179 value_bytes, &size) == ERROR_SUCCESS) { | |
| 180 *breakpad_enabled = value != 0; | |
| 181 } | |
| 182 ::RegCloseKey(key); | |
| 183 return size == sizeof(value); | |
| 184 } | |
| 185 | |
| 186 if (::RegOpenKeyEx(HKEY_CURRENT_USER, kRegPathChromePolicy, 0, | |
| 187 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { | |
| 188 if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type, | |
| 189 value_bytes, &size) == ERROR_SUCCESS) { | |
| 190 *breakpad_enabled = value != 0; | |
| 191 } | |
| 192 ::RegCloseKey(key); | |
| 193 return size == sizeof(value); | |
| 194 } | |
| 195 | |
| 196 return false; | |
| 197 } | |
| 198 | |
| 199 void InitializeProcessType() { | |
| 200 assert(g_process_type == ProcessType::UNINITIALIZED); | |
| 201 typedef bool (*IsSandboxedProcessFunc)(); | |
| 202 IsSandboxedProcessFunc is_sandboxed_process_func = | |
| 203 reinterpret_cast<IsSandboxedProcessFunc>( | |
| 204 GetProcAddress(GetModuleHandle(NULL), "IsSandboxedProcess")); | |
| 205 if (is_sandboxed_process_func && is_sandboxed_process_func()) { | |
| 206 g_process_type = ProcessType::NON_BROWSER_PROCESS; | |
| 207 return; | |
| 208 } | |
| 209 | |
| 210 // TODO(robertshield): Drop the command line check when we drop support for | |
| 211 // enabling chrome_elf in unsandboxed processes. | |
| 212 const wchar_t* command_line = GetCommandLine(); | |
| 213 if (command_line && wcsstr(command_line, L"--type")) { | |
| 214 g_process_type = ProcessType::NON_BROWSER_PROCESS; | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 g_process_type = ProcessType::BROWSER_PROCESS; | |
| 219 } | |
| 220 | |
| 221 bool IsNonBrowserProcess() { | |
| 222 assert(g_process_type != ProcessType::UNINITIALIZED); | |
| 223 return g_process_type == ProcessType::NON_BROWSER_PROCESS; | |
| 224 } | |
| OLD | NEW |