| 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 "base/win/iat_patch_function.h" | 5 #include "base/win/iat_patch_function.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/win/pe_image.h" | 8 #include "base/win/pe_image.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 // length Number of bytes to copy | 49 // length Number of bytes to copy |
| 50 // | 50 // |
| 51 // Returns: Windows error code (winerror.h). NO_ERROR if successful | 51 // Returns: Windows error code (winerror.h). NO_ERROR if successful |
| 52 DWORD ModifyCode(void* old_code, void* new_code, int length) { | 52 DWORD ModifyCode(void* old_code, void* new_code, int length) { |
| 53 if ((NULL == old_code) || (NULL == new_code) || (0 == length)) { | 53 if ((NULL == old_code) || (NULL == new_code) || (0 == length)) { |
| 54 NOTREACHED(); | 54 NOTREACHED(); |
| 55 return ERROR_INVALID_PARAMETER; | 55 return ERROR_INVALID_PARAMETER; |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Change the page protection so that we can write. | 58 // Change the page protection so that we can write. |
| 59 MEMORY_BASIC_INFORMATION memory_info; |
| 59 DWORD error = NO_ERROR; | 60 DWORD error = NO_ERROR; |
| 60 DWORD old_page_protection = 0; | 61 DWORD old_page_protection = 0; |
| 62 |
| 63 if (!VirtualQuery(old_code, &memory_info, sizeof(memory_info))) { |
| 64 error = GetLastError(); |
| 65 return error; |
| 66 } |
| 67 |
| 68 DWORD is_executable = (PAGE_EXECUTE | PAGE_EXECUTE_READ | |
| 69 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) & |
| 70 memory_info.Protect; |
| 71 |
| 61 if (VirtualProtect(old_code, | 72 if (VirtualProtect(old_code, |
| 62 length, | 73 length, |
| 63 PAGE_READWRITE, | 74 is_executable ? PAGE_EXECUTE_READWRITE : |
| 75 PAGE_READWRITE, |
| 64 &old_page_protection)) { | 76 &old_page_protection)) { |
| 65 | 77 |
| 66 // Write the data. | 78 // Write the data. |
| 67 CopyMemory(old_code, new_code, length); | 79 CopyMemory(old_code, new_code, length); |
| 68 | 80 |
| 69 // Restore the old page protection. | 81 // Restore the old page protection. |
| 70 error = ERROR_SUCCESS; | 82 error = ERROR_SUCCESS; |
| 71 VirtualProtect(old_code, | 83 VirtualProtect(old_code, |
| 72 length, | 84 length, |
| 73 old_page_protection, | 85 old_page_protection, |
| 74 &old_page_protection); | 86 &old_page_protection); |
| 75 } else { | 87 } else { |
| 76 error = GetLastError(); | 88 error = GetLastError(); |
| 77 NOTREACHED(); | |
| 78 } | 89 } |
| 79 | 90 |
| 80 return error; | 91 return error; |
| 81 } | 92 } |
| 82 | 93 |
| 83 bool InterceptEnumCallback(const base::win::PEImage& image, const char* module, | 94 bool InterceptEnumCallback(const base::win::PEImage& image, const char* module, |
| 84 DWORD ordinal, const char* name, DWORD hint, | 95 DWORD ordinal, const char* name, DWORD hint, |
| 85 IMAGE_THUNK_DATA* iat, void* cookie) { | 96 IMAGE_THUNK_DATA* iat, void* cookie) { |
| 86 InterceptFunctionInformation* intercept_information = | 97 InterceptFunctionInformation* intercept_information = |
| 87 reinterpret_cast<InterceptFunctionInformation*>(cookie); | 98 reinterpret_cast<InterceptFunctionInformation*>(cookie); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 module_handle_ = NULL; | 280 module_handle_ = NULL; |
| 270 intercept_function_ = NULL; | 281 intercept_function_ = NULL; |
| 271 original_function_ = NULL; | 282 original_function_ = NULL; |
| 272 iat_thunk_ = NULL; | 283 iat_thunk_ = NULL; |
| 273 | 284 |
| 274 return error; | 285 return error; |
| 275 } | 286 } |
| 276 | 287 |
| 277 } // namespace win | 288 } // namespace win |
| 278 } // namespace base | 289 } // namespace base |
| OLD | NEW |