OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "sandbox/win/src/win_utils.h" | 5 #include "sandbox/win/src/win_utils.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 HANDLE file = ::CreateFileW(path.c_str(), 0, | 269 HANDLE file = ::CreateFileW(path.c_str(), 0, |
270 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, | 270 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
271 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | 271 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
272 if (file == INVALID_HANDLE_VALUE) | 272 if (file == INVALID_HANDLE_VALUE) |
273 return false; | 273 return false; |
274 bool rv = GetPathFromHandle(file, nt_path); | 274 bool rv = GetPathFromHandle(file, nt_path); |
275 ::CloseHandle(file); | 275 ::CloseHandle(file); |
276 return rv; | 276 return rv; |
277 } | 277 } |
278 | 278 |
279 bool WriteProtectedChildMemory(HANDLE child_process, void* address, | |
280 const void* buffer, size_t length) { | |
281 // First, remove the protections. | |
282 DWORD old_protection; | |
283 if (!::VirtualProtectEx(child_process, address, length, | |
284 PAGE_WRITECOPY, &old_protection)) | |
285 return false; | |
286 | |
287 SIZE_T written; | |
288 bool ok = ::WriteProcessMemory(child_process, address, buffer, length, | |
289 &written) && (length == written); | |
290 | |
291 // Always attempt to restore the original protection. | |
292 if (!::VirtualProtectEx(child_process, address, length, | |
293 old_protection, &old_protection)) | |
294 return false; | |
295 | |
296 return ok; | |
297 } | |
298 | |
299 }; // namespace sandbox | 279 }; // namespace sandbox |
300 | 280 |
301 // TODO(jschuh): http://crbug.com/11789 | 281 // TODO(jschuh): http://crbug.com/11789 |
302 // I'm guessing we have a race where some "security" software is messing | 282 // I'm guessing we have a race where some "security" software is messing |
303 // with ntdll/imports underneath us. So, we retry a few times, and in the | 283 // with ntdll/imports underneath us. So, we retry a few times, and in the |
304 // worst case we sleep briefly before a few more attempts. (Normally sleeping | 284 // worst case we sleep briefly before a few more attempts. (Normally sleeping |
305 // would be very bad, but it's better than crashing in this case.) | 285 // would be very bad, but it's better than crashing in this case.) |
306 void ResolveNTFunctionPtr(const char* name, void* ptr) { | 286 void ResolveNTFunctionPtr(const char* name, void* ptr) { |
307 const int max_tries = 5; | 287 const int max_tries = 5; |
308 const int sleep_threshold = 2; | 288 const int sleep_threshold = 2; |
309 | 289 |
310 static HMODULE ntdll = ::GetModuleHandle(sandbox::kNtdllName); | 290 static HMODULE ntdll = ::GetModuleHandle(sandbox::kNtdllName); |
311 | 291 |
312 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); | 292 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); |
313 *function_ptr = ::GetProcAddress(ntdll, name); | 293 *function_ptr = ::GetProcAddress(ntdll, name); |
314 | 294 |
315 for (int tries = 1; !(*function_ptr) && tries < max_tries; ++tries) { | 295 for (int tries = 1; !(*function_ptr) && tries < max_tries; ++tries) { |
316 if (tries >= sleep_threshold) | 296 if (tries >= sleep_threshold) |
317 ::Sleep(1); | 297 ::Sleep(1); |
318 ntdll = ::GetModuleHandle(sandbox::kNtdllName); | 298 ntdll = ::GetModuleHandle(sandbox::kNtdllName); |
319 *function_ptr = ::GetProcAddress(ntdll, name); | 299 *function_ptr = ::GetProcAddress(ntdll, name); |
320 } | 300 } |
321 | 301 |
322 CHECK(*function_ptr); | 302 CHECK(*function_ptr); |
323 } | 303 } |
OLD | NEW |