eat_resolver.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "sandbox/win/src/eat_resolver.h"
  5. #include <stddef.h>
  6. #include "base/win/pe_image.h"
  7. #include "sandbox/win/src/sandbox_nt_util.h"
  8. namespace sandbox {
  9. NTSTATUS EatResolverThunk::Setup(const void* target_module,
  10. const void* interceptor_module,
  11. const char* target_name,
  12. const char* interceptor_name,
  13. const void* interceptor_entry_point,
  14. void* thunk_storage,
  15. size_t storage_bytes,
  16. size_t* storage_used) {
  17. NTSTATUS ret =
  18. Init(target_module, interceptor_module, target_name, interceptor_name,
  19. interceptor_entry_point, thunk_storage, storage_bytes);
  20. if (!NT_SUCCESS(ret))
  21. return ret;
  22. if (!eat_entry_)
  23. return STATUS_INVALID_PARAMETER;
  24. #if defined(_WIN64)
  25. // We have two thunks, in order: the return path and the forward path.
  26. if (!SetInternalThunk(thunk_storage, storage_bytes, nullptr, target_))
  27. return STATUS_BUFFER_TOO_SMALL;
  28. size_t thunk_bytes = GetInternalThunkSize();
  29. storage_bytes -= thunk_bytes;
  30. thunk_storage = reinterpret_cast<char*>(thunk_storage) + thunk_bytes;
  31. #endif
  32. if (!SetInternalThunk(thunk_storage, storage_bytes, target_, interceptor_))
  33. return STATUS_BUFFER_TOO_SMALL;
  34. AutoProtectMemory memory;
  35. ret = memory.ChangeProtection(eat_entry_, sizeof(DWORD), PAGE_READWRITE);
  36. if (!NT_SUCCESS(ret))
  37. return ret;
  38. // Perform the patch.
  39. *eat_entry_ = static_cast<DWORD>(reinterpret_cast<uintptr_t>(thunk_storage)) -
  40. static_cast<DWORD>(reinterpret_cast<uintptr_t>(target_module));
  41. if (storage_used)
  42. *storage_used = GetThunkSize();
  43. return ret;
  44. }
  45. NTSTATUS EatResolverThunk::ResolveTarget(const void* module,
  46. const char* function_name,
  47. void** address) {
  48. DCHECK_NT(address);
  49. if (!module)
  50. return STATUS_INVALID_PARAMETER;
  51. base::win::PEImage pe(module);
  52. if (!pe.VerifyMagic())
  53. return STATUS_INVALID_IMAGE_FORMAT;
  54. eat_entry_ = pe.GetExportEntry(function_name);
  55. if (!eat_entry_)
  56. return STATUS_PROCEDURE_NOT_FOUND;
  57. *address = pe.RVAToAddr(*eat_entry_);
  58. return STATUS_SUCCESS;
  59. }
  60. size_t EatResolverThunk::GetThunkSize() const {
  61. #if defined(_WIN64)
  62. return GetInternalThunkSize() * 2;
  63. #else
  64. return GetInternalThunkSize();
  65. #endif
  66. }
  67. } // namespace sandbox