michael@0: // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "sandbox/win/src/eat_resolver.h" michael@0: michael@0: #include "base/win/pe_image.h" michael@0: #include "sandbox/win/src/sandbox_nt_util.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: NTSTATUS EatResolverThunk::Setup(const void* target_module, michael@0: const void* interceptor_module, michael@0: const char* target_name, michael@0: const char* interceptor_name, michael@0: const void* interceptor_entry_point, michael@0: void* thunk_storage, michael@0: size_t storage_bytes, michael@0: size_t* storage_used) { michael@0: NTSTATUS ret = Init(target_module, interceptor_module, target_name, michael@0: interceptor_name, interceptor_entry_point, michael@0: thunk_storage, storage_bytes); michael@0: if (!NT_SUCCESS(ret)) michael@0: return ret; michael@0: michael@0: if (!eat_entry_) michael@0: return STATUS_INVALID_PARAMETER; michael@0: michael@0: size_t thunk_bytes = GetInternalThunkSize(); michael@0: michael@0: #if defined(_WIN64) michael@0: // We have two thunks, in order: the return path and the forward path. michael@0: if (!SetInternalThunk(thunk_storage, storage_bytes, NULL, target_)) michael@0: return STATUS_BUFFER_TOO_SMALL; michael@0: michael@0: storage_bytes -= thunk_bytes; michael@0: thunk_storage = reinterpret_cast(thunk_storage) + thunk_bytes; michael@0: #endif michael@0: michael@0: if (!SetInternalThunk(thunk_storage, storage_bytes, target_, interceptor_)) michael@0: return STATUS_BUFFER_TOO_SMALL; michael@0: michael@0: AutoProtectMemory memory; michael@0: ret = memory.ChangeProtection(eat_entry_, sizeof(DWORD), PAGE_READWRITE); michael@0: if (!NT_SUCCESS(ret)) michael@0: return ret; michael@0: michael@0: // Perform the patch. michael@0: #pragma warning(push) michael@0: #pragma warning(disable: 4311) michael@0: // These casts generate warnings because they are 32 bit specific. michael@0: *eat_entry_ = reinterpret_cast(thunk_storage) - michael@0: reinterpret_cast(target_module); michael@0: #pragma warning(pop) michael@0: michael@0: if (NULL != storage_used) michael@0: *storage_used = GetThunkSize(); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: NTSTATUS EatResolverThunk::ResolveTarget(const void* module, michael@0: const char* function_name, michael@0: void** address) { michael@0: DCHECK_NT(address); michael@0: if (!module) michael@0: return STATUS_INVALID_PARAMETER; michael@0: michael@0: base::win::PEImage pe(module); michael@0: if (!pe.VerifyMagic()) michael@0: return STATUS_INVALID_IMAGE_FORMAT; michael@0: michael@0: eat_entry_ = pe.GetExportEntry(function_name); michael@0: michael@0: if (!eat_entry_) michael@0: return STATUS_PROCEDURE_NOT_FOUND; michael@0: michael@0: *address = pe.RVAToAddr(*eat_entry_); michael@0: michael@0: return STATUS_SUCCESS; michael@0: } michael@0: michael@0: size_t EatResolverThunk::GetThunkSize() const { michael@0: #if defined(_WIN64) michael@0: return GetInternalThunkSize() * 2; michael@0: #else michael@0: return GetInternalThunkSize(); michael@0: #endif michael@0: } michael@0: michael@0: } // namespace sandbox