service_resolver.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) 2012 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. #ifndef SANDBOX_WIN_SRC_SERVICE_RESOLVER_H_
  5. #define SANDBOX_WIN_SRC_SERVICE_RESOLVER_H_
  6. #include <stddef.h>
  7. #include "sandbox/win/src/nt_internals.h"
  8. #include "sandbox/win/src/resolver.h"
  9. namespace sandbox {
  10. // This is the concrete resolver used to perform service-call type functions
  11. // inside ntdll.dll.
  12. class ServiceResolverThunk : public ResolverThunk {
  13. public:
  14. // The service resolver needs a child process to write to.
  15. ServiceResolverThunk(HANDLE process, bool relaxed)
  16. : ntdll_base_(nullptr),
  17. process_(process),
  18. relaxed_(relaxed),
  19. relative_jump_(0) {}
  20. ServiceResolverThunk(const ServiceResolverThunk&) = delete;
  21. ServiceResolverThunk& operator=(const ServiceResolverThunk&) = delete;
  22. ~ServiceResolverThunk() override {}
  23. // Implementation of Resolver::Setup.
  24. NTSTATUS Setup(const void* target_module,
  25. const void* interceptor_module,
  26. const char* target_name,
  27. const char* interceptor_name,
  28. const void* interceptor_entry_point,
  29. void* thunk_storage,
  30. size_t storage_bytes,
  31. size_t* storage_used) override;
  32. // Implementation of Resolver::ResolveInterceptor.
  33. NTSTATUS ResolveInterceptor(const void* module,
  34. const char* function_name,
  35. const void** address) override;
  36. // Implementation of Resolver::ResolveTarget.
  37. NTSTATUS ResolveTarget(const void* module,
  38. const char* function_name,
  39. void** address) override;
  40. // Implementation of Resolver::GetThunkSize.
  41. size_t GetThunkSize() const override;
  42. // Call this to set up ntdll_base_ which will allow for local patches.
  43. virtual void AllowLocalPatches();
  44. // Verifies that the function specified by |target_name| in |target_module| is
  45. // a service and copies the data from that function into |thunk_storage|. If
  46. // |storage_bytes| is too small, then the method fails.
  47. virtual NTSTATUS CopyThunk(const void* target_module,
  48. const char* target_name,
  49. BYTE* thunk_storage,
  50. size_t storage_bytes,
  51. size_t* storage_used);
  52. protected:
  53. // The unit test will use this member to allow local patch on a buffer.
  54. HMODULE ntdll_base_;
  55. // Handle of the child process.
  56. HANDLE process_;
  57. private:
  58. // Returns true if the code pointer by target_ corresponds to the expected
  59. // type of function. Saves that code on the first part of the thunk pointed
  60. // by local_thunk (should be directly accessible from the parent).
  61. virtual bool IsFunctionAService(void* local_thunk) const;
  62. // Performs the actual patch of target_.
  63. // local_thunk must be already fully initialized, and the first part must
  64. // contain the original code. The real type of this buffer is ServiceFullThunk
  65. // (yes, private). remote_thunk (real type ServiceFullThunk), must be
  66. // allocated on the child, and will contain the thunk data, after this call.
  67. // Returns the apropriate status code.
  68. virtual NTSTATUS PerformPatch(void* local_thunk, void* remote_thunk);
  69. // Provides basically the same functionality as IsFunctionAService but it
  70. // continues even if it does not recognize the function code. remote_thunk
  71. // is the address of our memory on the child.
  72. bool SaveOriginalFunction(void* local_thunk, void* remote_thunk);
  73. // true if we are allowed to patch already-patched functions.
  74. bool relaxed_;
  75. ULONG relative_jump_;
  76. };
  77. // This is the concrete resolver used to perform service-call type functions
  78. // inside ntdll.dll on WOW64 (32 bit ntdll on 64 bit Vista).
  79. class Wow64ResolverThunk : public ServiceResolverThunk {
  80. public:
  81. // The service resolver needs a child process to write to.
  82. Wow64ResolverThunk(HANDLE process, bool relaxed)
  83. : ServiceResolverThunk(process, relaxed) {}
  84. Wow64ResolverThunk(const Wow64ResolverThunk&) = delete;
  85. Wow64ResolverThunk& operator=(const Wow64ResolverThunk&) = delete;
  86. ~Wow64ResolverThunk() override {}
  87. private:
  88. bool IsFunctionAService(void* local_thunk) const override;
  89. };
  90. // This is the concrete resolver used to perform service-call type functions
  91. // inside ntdll.dll on WOW64 for Windows 8.
  92. class Wow64W8ResolverThunk : public ServiceResolverThunk {
  93. public:
  94. // The service resolver needs a child process to write to.
  95. Wow64W8ResolverThunk(HANDLE process, bool relaxed)
  96. : ServiceResolverThunk(process, relaxed) {}
  97. Wow64W8ResolverThunk(const Wow64W8ResolverThunk&) = delete;
  98. Wow64W8ResolverThunk& operator=(const Wow64W8ResolverThunk&) = delete;
  99. ~Wow64W8ResolverThunk() override {}
  100. private:
  101. bool IsFunctionAService(void* local_thunk) const override;
  102. };
  103. // This is the concrete resolver used to perform service-call type functions
  104. // inside ntdll.dll on Windows 8.
  105. class Win8ResolverThunk : public ServiceResolverThunk {
  106. public:
  107. // The service resolver needs a child process to write to.
  108. Win8ResolverThunk(HANDLE process, bool relaxed)
  109. : ServiceResolverThunk(process, relaxed) {}
  110. Win8ResolverThunk(const Win8ResolverThunk&) = delete;
  111. Win8ResolverThunk& operator=(const Win8ResolverThunk&) = delete;
  112. ~Win8ResolverThunk() override {}
  113. private:
  114. bool IsFunctionAService(void* local_thunk) const override;
  115. };
  116. // This is the concrete resolver used to perform service-call type functions
  117. // inside ntdll.dll on WOW64 for Windows 10.
  118. class Wow64W10ResolverThunk : public ServiceResolverThunk {
  119. public:
  120. // The service resolver needs a child process to write to.
  121. Wow64W10ResolverThunk(HANDLE process, bool relaxed)
  122. : ServiceResolverThunk(process, relaxed) {}
  123. Wow64W10ResolverThunk(const Wow64W10ResolverThunk&) = delete;
  124. Wow64W10ResolverThunk& operator=(const Wow64W10ResolverThunk&) = delete;
  125. ~Wow64W10ResolverThunk() override {}
  126. private:
  127. bool IsFunctionAService(void* local_thunk) const override;
  128. };
  129. } // namespace sandbox
  130. #endif // SANDBOX_WIN_SRC_SERVICE_RESOLVER_H_