OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome_elf/blacklist/blacklist.h" | 5 #include "chrome_elf/blacklist/blacklist.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "chrome_elf/blacklist/blacklist_interceptions.h" | 10 #include "chrome_elf/blacklist/blacklist_interceptions.h" |
11 #include "sandbox/win/src/interception_internal.h" | 11 #include "sandbox/win/src/interception_internal.h" |
12 #include "sandbox/win/src/internal_types.h" | 12 #include "sandbox/win/src/internal_types.h" |
13 #include "sandbox/win/src/sandbox_utils.h" | 13 #include "sandbox/win/src/sandbox_utils.h" |
14 #include "sandbox/win/src/service_resolver.h" | 14 #include "sandbox/win/src/service_resolver.h" |
| 15 #include "version.h" // NOLINT |
15 | 16 |
16 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx | 17 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
17 extern "C" IMAGE_DOS_HEADER __ImageBase; | 18 extern "C" IMAGE_DOS_HEADER __ImageBase; |
18 | 19 |
19 namespace blacklist{ | 20 namespace blacklist{ |
20 | 21 |
21 const wchar_t* g_troublesome_dlls[kTroublesomeDllsMaxCount] = {}; | 22 const wchar_t* g_troublesome_dlls[kTroublesomeDllsMaxCount] = {}; |
22 int g_troublesome_dlls_cur_index = 0; | 23 int g_troublesome_dlls_cur_index = 0; |
23 | 24 |
24 const wchar_t kRegistryBeaconPath[] = L"SOFTWARE\\Google\\Chrome\\BLBeacon"; | 25 const wchar_t kRegistryBeaconPath[] = L"SOFTWARE\\Google\\Chrome\\BLBeacon"; |
| 26 const wchar_t kBeaconVersion[] = L"version"; |
| 27 const wchar_t kBeaconState[] = L"state"; |
25 | 28 |
26 } // namespace blacklist | 29 } // namespace blacklist |
27 | 30 |
28 // Allocate storage for thunks in a page of this module to save on doing | 31 // Allocate storage for thunks in a page of this module to save on doing |
29 // an extra allocation at run time. | 32 // an extra allocation at run time. |
30 #pragma section(".crthunk",read,execute) | 33 #pragma section(".crthunk",read,execute) |
31 __declspec(allocate(".crthunk")) sandbox::ThunkData g_thunk_storage; | 34 __declspec(allocate(".crthunk")) sandbox::ThunkData g_thunk_storage; |
32 | 35 |
33 namespace { | 36 namespace { |
34 | 37 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 136 |
134 bool IsNonBrowserProcess() { | 137 bool IsNonBrowserProcess() { |
135 wchar_t* command_line = GetCommandLine(); | 138 wchar_t* command_line = GetCommandLine(); |
136 return (command_line && wcsstr(command_line, L"--type")); | 139 return (command_line && wcsstr(command_line, L"--type")); |
137 } | 140 } |
138 | 141 |
139 } // namespace | 142 } // namespace |
140 | 143 |
141 namespace blacklist { | 144 namespace blacklist { |
142 | 145 |
143 bool CreateBeacon() { | 146 bool LeaveSetupBeacon() { |
144 HKEY beacon_key = NULL; | 147 HKEY key = NULL; |
145 DWORD disposition = 0; | 148 DWORD disposition = 0; |
146 LONG result = ::RegCreateKeyEx(HKEY_CURRENT_USER, | 149 LONG result = ::RegCreateKeyEx(HKEY_CURRENT_USER, |
147 kRegistryBeaconPath, | 150 kRegistryBeaconPath, |
148 0, | 151 0, |
149 NULL, | 152 NULL, |
150 0, | 153 REG_OPTION_NON_VOLATILE, |
151 KEY_WRITE, | 154 KEY_QUERY_VALUE | KEY_SET_VALUE, |
152 NULL, | 155 NULL, |
153 &beacon_key, | 156 &key, |
154 &disposition); | 157 &disposition); |
155 bool success = (result == ERROR_SUCCESS && | 158 if (result != ERROR_SUCCESS) |
156 disposition != REG_OPENED_EXISTING_KEY); | 159 return false; |
157 if (result == ERROR_SUCCESS) | |
158 ::RegCloseKey(beacon_key); | |
159 return success; | |
160 } | |
161 | 160 |
162 bool ClearBeacon() { | 161 // Retrieve the current blacklist state. |
163 LONG result = ::RegDeleteKey(HKEY_CURRENT_USER, kRegistryBeaconPath); | 162 DWORD blacklist_state = BLACKLIST_DISABLED; |
| 163 DWORD blacklist_state_size = sizeof(blacklist_state); |
| 164 DWORD type = 0; |
| 165 result = ::RegQueryValueEx(key, |
| 166 kBeaconState, |
| 167 0, |
| 168 &type, |
| 169 reinterpret_cast<LPBYTE>(&blacklist_state), |
| 170 &blacklist_state_size); |
| 171 |
| 172 if (blacklist_state != BLACKLIST_ENABLED || |
| 173 result != ERROR_SUCCESS || type != REG_DWORD) { |
| 174 ::RegCloseKey(key); |
| 175 return false; |
| 176 } |
| 177 |
| 178 // If the blacklist wasn't set as enabled for this version, don't |
| 179 // use it. |
| 180 wchar_t key_data[255] = {}; |
| 181 DWORD key_data_size = sizeof(key_data); |
| 182 result = ::RegQueryValueEx(key, |
| 183 blacklist::kBeaconVersion, |
| 184 0, |
| 185 &type, |
| 186 reinterpret_cast<LPBYTE>(key_data), |
| 187 &key_data_size); |
| 188 |
| 189 if (wcscmp(key_data, TEXT(CHROME_VERSION_STRING)) != 0 || |
| 190 result != ERROR_SUCCESS || type != REG_SZ) { |
| 191 ::RegCloseKey(key); |
| 192 return false; |
| 193 } |
| 194 |
| 195 // Mark the blacklist setup code as running so if it crashes the blacklist |
| 196 // won't be enabled for the next run. |
| 197 blacklist_state = BLACKLIST_SETUP_RUNNING; |
| 198 result = ::RegSetValueEx(key, |
| 199 kBeaconState, |
| 200 0, |
| 201 REG_DWORD, |
| 202 reinterpret_cast<LPBYTE>(&blacklist_state), |
| 203 sizeof(blacklist_state)); |
| 204 ::RegCloseKey(key); |
| 205 |
164 return (result == ERROR_SUCCESS); | 206 return (result == ERROR_SUCCESS); |
165 } | 207 } |
166 | 208 |
| 209 bool ResetBeacon() { |
| 210 HKEY key = NULL; |
| 211 DWORD disposition = 0; |
| 212 LONG result = ::RegCreateKeyEx(HKEY_CURRENT_USER, |
| 213 kRegistryBeaconPath, |
| 214 0, |
| 215 NULL, |
| 216 REG_OPTION_NON_VOLATILE, |
| 217 KEY_QUERY_VALUE | KEY_SET_VALUE, |
| 218 NULL, |
| 219 &key, |
| 220 &disposition); |
| 221 if (result != ERROR_SUCCESS) |
| 222 return false; |
| 223 |
| 224 DWORD blacklist_state = BLACKLIST_ENABLED; |
| 225 result = ::RegSetValueEx(key, |
| 226 kBeaconState, |
| 227 0, |
| 228 REG_DWORD, |
| 229 reinterpret_cast<LPBYTE>(&blacklist_state), |
| 230 sizeof(blacklist_state)); |
| 231 ::RegCloseKey(key); |
| 232 |
| 233 return (result == ERROR_SUCCESS); |
| 234 } |
| 235 |
167 bool AddDllToBlacklist(const wchar_t* dll_name) { | 236 bool AddDllToBlacklist(const wchar_t* dll_name) { |
168 if (g_troublesome_dlls_cur_index >= kTroublesomeDllsMaxCount) | 237 if (g_troublesome_dlls_cur_index >= kTroublesomeDllsMaxCount) |
169 return false; | 238 return false; |
170 for (int i = 0; i < g_troublesome_dlls_cur_index; ++i) { | 239 for (int i = 0; i < g_troublesome_dlls_cur_index; ++i) { |
171 if (!wcscmp(g_troublesome_dlls[i], dll_name)) | 240 if (!wcscmp(g_troublesome_dlls[i], dll_name)) |
172 return true; | 241 return true; |
173 } | 242 } |
174 | 243 |
175 // Copy string to blacklist. | 244 // Copy string to blacklist. |
176 wchar_t* str_buffer = new wchar_t[wcslen(dll_name) + 1]; | 245 wchar_t* str_buffer = new wchar_t[wcslen(dll_name) + 1]; |
(...skipping 28 matching lines...) Expand all Loading... |
205 | 274 |
206 // Check to see that we found the functions we need in ntdll. | 275 // Check to see that we found the functions we need in ntdll. |
207 if (!InitializeInterceptImports()) | 276 if (!InitializeInterceptImports()) |
208 return false; | 277 return false; |
209 | 278 |
210 // Check to see if this is a non-browser process, abort if so. | 279 // Check to see if this is a non-browser process, abort if so. |
211 if (IsNonBrowserProcess()) | 280 if (IsNonBrowserProcess()) |
212 return false; | 281 return false; |
213 | 282 |
214 // Check to see if a beacon is present, abort if so. | 283 // Check to see if a beacon is present, abort if so. |
215 if (!force && !CreateBeacon()) | 284 if (!force && !LeaveSetupBeacon()) |
216 return false; | 285 return false; |
217 | 286 |
218 // Don't try blacklisting on unsupported OS versions. | 287 // Don't try blacklisting on unsupported OS versions. |
219 OSInfo os_info; | 288 OSInfo os_info; |
220 if (os_info.version() <= VERSION_PRE_XP_SP2) | 289 if (os_info.version() <= VERSION_PRE_XP_SP2) |
221 return false; | 290 return false; |
222 | 291 |
223 // Pseudo-handle, no need to close. | 292 // Pseudo-handle, no need to close. |
224 HANDLE current_process = ::GetCurrentProcess(); | 293 HANDLE current_process = ::GetCurrentProcess(); |
225 | 294 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 // Mark the thunk storage as executable and prevent any future writes to it. | 341 // Mark the thunk storage as executable and prevent any future writes to it. |
273 BOOL page_executable = VirtualProtect(&g_thunk_storage, | 342 BOOL page_executable = VirtualProtect(&g_thunk_storage, |
274 sizeof(g_thunk_storage), | 343 sizeof(g_thunk_storage), |
275 PAGE_EXECUTE_READ, | 344 PAGE_EXECUTE_READ, |
276 &old_protect); | 345 &old_protect); |
277 | 346 |
278 return NT_SUCCESS(ret) && page_executable; | 347 return NT_SUCCESS(ret) && page_executable; |
279 } | 348 } |
280 | 349 |
281 } // namespace blacklist | 350 } // namespace blacklist |
OLD | NEW |