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; | |
60 DWORD error = NO_ERROR; | 59 DWORD error = NO_ERROR; |
61 DWORD old_page_protection = 0; | 60 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 | |
72 if (VirtualProtect(old_code, | 61 if (VirtualProtect(old_code, |
73 length, | 62 length, |
74 is_executable ? PAGE_EXECUTE_READWRITE : | 63 PAGE_READWRITE, |
75 PAGE_READWRITE, | |
76 &old_page_protection)) { | 64 &old_page_protection)) { |
77 | 65 |
78 // Write the data. | 66 // Write the data. |
79 CopyMemory(old_code, new_code, length); | 67 CopyMemory(old_code, new_code, length); |
80 | 68 |
81 // Restore the old page protection. | 69 // Restore the old page protection. |
82 error = ERROR_SUCCESS; | 70 error = ERROR_SUCCESS; |
83 VirtualProtect(old_code, | 71 VirtualProtect(old_code, |
84 length, | 72 length, |
85 old_page_protection, | 73 old_page_protection, |
86 &old_page_protection); | 74 &old_page_protection); |
87 } else { | 75 } else { |
88 error = GetLastError(); | 76 error = GetLastError(); |
| 77 NOTREACHED(); |
89 } | 78 } |
90 | 79 |
91 return error; | 80 return error; |
92 } | 81 } |
93 | 82 |
94 bool InterceptEnumCallback(const base::win::PEImage& image, const char* module, | 83 bool InterceptEnumCallback(const base::win::PEImage& image, const char* module, |
95 DWORD ordinal, const char* name, DWORD hint, | 84 DWORD ordinal, const char* name, DWORD hint, |
96 IMAGE_THUNK_DATA* iat, void* cookie) { | 85 IMAGE_THUNK_DATA* iat, void* cookie) { |
97 InterceptFunctionInformation* intercept_information = | 86 InterceptFunctionInformation* intercept_information = |
98 reinterpret_cast<InterceptFunctionInformation*>(cookie); | 87 reinterpret_cast<InterceptFunctionInformation*>(cookie); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 module_handle_ = NULL; | 269 module_handle_ = NULL; |
281 intercept_function_ = NULL; | 270 intercept_function_ = NULL; |
282 original_function_ = NULL; | 271 original_function_ = NULL; |
283 iat_thunk_ = NULL; | 272 iat_thunk_ = NULL; |
284 | 273 |
285 return error; | 274 return error; |
286 } | 275 } |
287 | 276 |
288 } // namespace win | 277 } // namespace win |
289 } // namespace base | 278 } // namespace base |
OLD | NEW |