Commit 2d50b3d7 authored by Robert Liao's avatar Robert Liao Committed by Commit Bot

Refactor ModifyCode out of iat_patch_function

This allows other callers to use it without including iat_patch_function.h.
Principally, this will be used for the upcoming CoCreateInstance DCHECK.

BUG=708303
TBR=gab@chromium.org
Refactor. No impact to running code.

Change-Id: Ibd25de76a2e9bf858619c6fccc18a46704237067
Reviewed-on: https://chromium-review.googlesource.com/531668
Commit-Queue: Robert Liao <robliao@chromium.org>
Reviewed-by: default avatarScott Graham <scottmg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#478789}
parent aed90520
...@@ -1057,6 +1057,8 @@ component("base") { ...@@ -1057,6 +1057,8 @@ component("base") {
"win/message_window.h", "win/message_window.h",
"win/object_watcher.cc", "win/object_watcher.cc",
"win/object_watcher.h", "win/object_watcher.h",
"win/patch_util.cc",
"win/patch_util.h",
"win/process_startup_helper.cc", "win/process_startup_helper.cc",
"win/process_startup_helper.h", "win/process_startup_helper.h",
"win/registry.cc", "win/registry.cc",
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/win/iat_patch_function.h" #include "base/win/iat_patch_function.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/win/patch_util.h"
#include "base/win/pe_image.h" #include "base/win/pe_image.h"
namespace base { namespace base {
...@@ -72,10 +73,9 @@ bool InterceptEnumCallback(const base::win::PEImage& image, const char* module, ...@@ -72,10 +73,9 @@ bool InterceptEnumCallback(const base::win::PEImage& image, const char* module,
"unknown IAT thunk format"); "unknown IAT thunk format");
// Patch the function. // Patch the function.
intercept_information->return_code = intercept_information->return_code = internal::ModifyCode(
ModifyCode(&(iat->u1.Function), &(iat->u1.Function), &(intercept_information->new_function),
&(intercept_information->new_function), sizeof(intercept_information->new_function));
sizeof(intercept_information->new_function));
// Terminate further enumeration. // Terminate further enumeration.
intercept_information->finished_operation = true; intercept_information->finished_operation = true;
...@@ -160,64 +160,12 @@ DWORD RestoreImportedFunction(void* intercept_function, ...@@ -160,64 +160,12 @@ DWORD RestoreImportedFunction(void* intercept_function,
return ERROR_INVALID_FUNCTION; return ERROR_INVALID_FUNCTION;
} }
return ModifyCode(&(iat_thunk->u1.Function), return internal::ModifyCode(&(iat_thunk->u1.Function), &original_function,
&original_function, sizeof(original_function));
sizeof(original_function));
} }
} // namespace } // namespace
// Change the page protection (of code pages) to writable and copy
// the data at the specified location
//
// Arguments:
// old_code Target location to copy
// new_code Source
// length Number of bytes to copy
//
// Returns: Windows error code (winerror.h). NO_ERROR if successful
DWORD ModifyCode(void* old_code, void* new_code, int length) {
if ((NULL == old_code) || (NULL == new_code) || (0 == length)) {
NOTREACHED();
return ERROR_INVALID_PARAMETER;
}
// Change the page protection so that we can write.
MEMORY_BASIC_INFORMATION memory_info;
DWORD error = NO_ERROR;
DWORD old_page_protection = 0;
if (!VirtualQuery(old_code, &memory_info, sizeof(memory_info))) {
error = GetLastError();
return error;
}
DWORD is_executable = (PAGE_EXECUTE | PAGE_EXECUTE_READ |
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) &
memory_info.Protect;
if (VirtualProtect(old_code,
length,
is_executable ? PAGE_EXECUTE_READWRITE :
PAGE_READWRITE,
&old_page_protection)) {
// Write the data.
CopyMemory(old_code, new_code, length);
// Restore the old page protection.
error = ERROR_SUCCESS;
VirtualProtect(old_code,
length,
old_page_protection,
&old_page_protection);
} else {
error = GetLastError();
}
return error;
}
IATPatchFunction::IATPatchFunction() IATPatchFunction::IATPatchFunction()
: module_handle_(NULL), : module_handle_(NULL),
intercept_function_(NULL), intercept_function_(NULL),
......
...@@ -75,8 +75,6 @@ class BASE_EXPORT IATPatchFunction { ...@@ -75,8 +75,6 @@ class BASE_EXPORT IATPatchFunction {
DISALLOW_COPY_AND_ASSIGN(IATPatchFunction); DISALLOW_COPY_AND_ASSIGN(IATPatchFunction);
}; };
BASE_EXPORT DWORD ModifyCode(void* old_code, void* new_code, int length);
} // namespace win } // namespace win
} // namespace base } // namespace base
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/patch_util.h"
#include "base/logging.h"
namespace base {
namespace win {
namespace internal {
DWORD ModifyCode(void* destination, const void* source, int length) {
if ((NULL == destination) || (NULL == source) || (0 == length)) {
NOTREACHED();
return ERROR_INVALID_PARAMETER;
}
// Change the page protection so that we can write.
MEMORY_BASIC_INFORMATION memory_info;
DWORD error = NO_ERROR;
DWORD old_page_protection = 0;
if (!VirtualQuery(destination, &memory_info, sizeof(memory_info))) {
error = GetLastError();
return error;
}
DWORD is_executable = (PAGE_EXECUTE | PAGE_EXECUTE_READ |
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) &
memory_info.Protect;
if (VirtualProtect(destination, length,
is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE,
&old_page_protection)) {
// Write the data.
CopyMemory(destination, source, length);
// Restore the old page protection.
error = ERROR_SUCCESS;
VirtualProtect(destination, length, old_page_protection,
&old_page_protection);
} else {
error = GetLastError();
}
return error;
}
} // namespace internal
} // namespace win
} // namespace bsae
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_PATCH_UTIL_H_
#define BASE_WIN_PATCH_UTIL_H_
#include <windows.h>
namespace base {
namespace win {
namespace internal {
// Copies |length| bytes from |source| to |destination|, temporarily setting
// |destination| to writable. Returns a Windows error code or NO_ERROR if
// successful.
DWORD ModifyCode(void* destination, const void* source, int length);
} // namespace internal
} // namespace win
} // namespace bsae
#endif // BASE_WIN_PATCH_UTIL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment