| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // For information about interceptions as a whole see | 5 // For information about interceptions as a whole see |
| 6 // http://dev.chromium.org/developers/design-documents/sandbox . | 6 // http://dev.chromium.org/developers/design-documents/sandbox . |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "sandbox/win/src/interception.h" | 10 #include "sandbox/win/src/interception.h" |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/win/pe_image.h" | 15 #include "base/win/pe_image.h" |
| 16 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
| 17 #include "sandbox/win/src/interception_internal.h" | 17 #include "sandbox/win/src/interception_internal.h" |
| 18 #include "sandbox/win/src/interceptors.h" | 18 #include "sandbox/win/src/interceptors.h" |
| 19 #include "sandbox/win/src/sandbox.h" | 19 #include "sandbox/win/src/sandbox.h" |
| 20 #include "sandbox/win/src/sandbox_rand.h" |
| 20 #include "sandbox/win/src/service_resolver.h" | 21 #include "sandbox/win/src/service_resolver.h" |
| 21 #include "sandbox/win/src/target_interceptions.h" | 22 #include "sandbox/win/src/target_interceptions.h" |
| 22 #include "sandbox/win/src/target_process.h" | 23 #include "sandbox/win/src/target_process.h" |
| 23 #include "sandbox/win/src/wow64.h" | 24 #include "sandbox/win/src/wow64.h" |
| 24 | 25 |
| 26 namespace sandbox { |
| 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 // Standard allocation granularity and page size for Windows. | 30 // Standard allocation granularity and page size for Windows. |
| 28 const size_t kAllocGranularity = 65536; | 31 const size_t kAllocGranularity = 65536; |
| 29 const size_t kPageSize = 4096; | 32 const size_t kPageSize = 4096; |
| 30 | 33 |
| 34 } // namespace |
| 35 |
| 36 namespace internal { |
| 37 |
| 31 // Find a random offset within 64k and aligned to ceil(log2(size)). | 38 // Find a random offset within 64k and aligned to ceil(log2(size)). |
| 32 size_t GetGranularAlignedRandomOffset(size_t size) { | 39 size_t GetGranularAlignedRandomOffset(size_t size) { |
| 33 CHECK_LE(size, kAllocGranularity); | 40 CHECK_LE(size, kAllocGranularity); |
| 34 unsigned int offset; | 41 unsigned int offset; |
| 35 | 42 |
| 36 do { | 43 do { |
| 37 rand_s(&offset); | 44 GetRandom(&offset); |
| 38 offset &= (kAllocGranularity - 1); | 45 offset &= (kAllocGranularity - 1); |
| 39 } while (offset > (kAllocGranularity - size)); | 46 } while (offset > (kAllocGranularity - size)); |
| 40 | 47 |
| 41 // Find an alignment between 64 and the page size (4096). | 48 // Find an alignment between 64 and the page size (4096). |
| 42 size_t align_size = kPageSize; | 49 size_t align_size = kPageSize; |
| 43 for (size_t new_size = align_size / 2; new_size >= size; new_size /= 2) { | 50 for (size_t new_size = align_size / 2; new_size >= size; new_size /= 2) { |
| 44 align_size = new_size; | 51 align_size = new_size; |
| 45 } | 52 } |
| 46 return offset & ~(align_size - 1); | 53 return offset & ~(align_size - 1); |
| 47 } | 54 } |
| 48 | 55 |
| 49 } // namespace | 56 } // namespace internal |
| 50 | |
| 51 namespace sandbox { | |
| 52 | 57 |
| 53 SANDBOX_INTERCEPT SharedMemory* g_interceptions; | 58 SANDBOX_INTERCEPT SharedMemory* g_interceptions; |
| 54 | 59 |
| 55 // Table of the unpatched functions that we intercept. Mapped from the parent. | 60 // Table of the unpatched functions that we intercept. Mapped from the parent. |
| 56 SANDBOX_INTERCEPT OriginalFunctions g_originals = { NULL }; | 61 SANDBOX_INTERCEPT OriginalFunctions g_originals = { NULL }; |
| 57 | 62 |
| 58 // Magic constant that identifies that this function is not to be patched. | 63 // Magic constant that identifies that this function is not to be patched. |
| 59 const char kUnloadDLLDummyFunction[] = "@"; | 64 const char kUnloadDLLDummyFunction[] = "@"; |
| 60 | 65 |
| 61 InterceptionManager::InterceptionData::InterceptionData() { | 66 InterceptionManager::InterceptionData::InterceptionData() { |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 392 |
| 388 // Reserve a full 64k memory range in the child process. | 393 // Reserve a full 64k memory range in the child process. |
| 389 HANDLE child = child_->Process(); | 394 HANDLE child = child_->Process(); |
| 390 BYTE* thunk_base = reinterpret_cast<BYTE*>( | 395 BYTE* thunk_base = reinterpret_cast<BYTE*>( |
| 391 ::VirtualAllocEx(child, NULL, kAllocGranularity, | 396 ::VirtualAllocEx(child, NULL, kAllocGranularity, |
| 392 MEM_RESERVE, PAGE_NOACCESS)); | 397 MEM_RESERVE, PAGE_NOACCESS)); |
| 393 | 398 |
| 394 // Find an aligned, random location within the reserved range. | 399 // Find an aligned, random location within the reserved range. |
| 395 size_t thunk_bytes = interceptions_.size() * sizeof(ThunkData) + | 400 size_t thunk_bytes = interceptions_.size() * sizeof(ThunkData) + |
| 396 sizeof(DllInterceptionData); | 401 sizeof(DllInterceptionData); |
| 397 size_t thunk_offset = GetGranularAlignedRandomOffset(thunk_bytes); | 402 size_t thunk_offset = internal::GetGranularAlignedRandomOffset(thunk_bytes); |
| 398 | 403 |
| 399 // Split the base and offset along page boundaries. | 404 // Split the base and offset along page boundaries. |
| 400 thunk_base += thunk_offset & ~(kPageSize - 1); | 405 thunk_base += thunk_offset & ~(kPageSize - 1); |
| 401 thunk_offset &= kPageSize - 1; | 406 thunk_offset &= kPageSize - 1; |
| 402 | 407 |
| 403 // Make an aligned, padded allocation, and move the pointer to our chunk. | 408 // Make an aligned, padded allocation, and move the pointer to our chunk. |
| 404 size_t thunk_bytes_padded = (thunk_bytes + kPageSize - 1) & ~(kPageSize - 1); | 409 size_t thunk_bytes_padded = (thunk_bytes + kPageSize - 1) & ~(kPageSize - 1); |
| 405 thunk_base = reinterpret_cast<BYTE*>( | 410 thunk_base = reinterpret_cast<BYTE*>( |
| 406 ::VirtualAllocEx(child, thunk_base, thunk_bytes_padded, | 411 ::VirtualAllocEx(child, thunk_base, thunk_bytes_padded, |
| 407 MEM_COMMIT, PAGE_EXECUTE_READWRITE)); | 412 MEM_COMMIT, PAGE_EXECUTE_READWRITE)); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 ::FreeLibrary(local_interceptor); | 547 ::FreeLibrary(local_interceptor); |
| 543 #endif | 548 #endif |
| 544 | 549 |
| 545 if (it != interceptions_.end()) | 550 if (it != interceptions_.end()) |
| 546 return false; | 551 return false; |
| 547 | 552 |
| 548 return true; | 553 return true; |
| 549 } | 554 } |
| 550 | 555 |
| 551 } // namespace sandbox | 556 } // namespace sandbox |
| OLD | NEW |