| OLD | NEW | 
 | (Empty) | 
|    1 // Copyright (c) 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  |  | 
|    5 // Defines ResolverThunk, the interface for classes that perform interceptions. |  | 
|    6 // For more details see |  | 
|    7 // http://dev.chromium.org/developers/design-documents/sandbox . |  | 
|    8  |  | 
|    9 #include <stddef.h> |  | 
|   10  |  | 
|   11 #include "base/macros.h" |  | 
|   12 #include "sandbox/win/src/nt_internals.h" |  | 
|   13  |  | 
|   14 #ifndef SANDBOX_SRC_RESOLVER_H__ |  | 
|   15 #define SANDBOX_SRC_RESOLVER_H__ |  | 
|   16  |  | 
|   17 namespace sandbox { |  | 
|   18  |  | 
|   19 // A resolver is the object in charge of performing the actual interception of |  | 
|   20 // a function. There should be a concrete implementation of a resolver roughly |  | 
|   21 // per type of interception. |  | 
|   22 class ResolverThunk { |  | 
|   23  public: |  | 
|   24   ResolverThunk() {} |  | 
|   25   virtual ~ResolverThunk() {} |  | 
|   26  |  | 
|   27   // Performs the actual interception of a function. |  | 
|   28   // target_name is an exported function from the module loaded at |  | 
|   29   // target_module, and must be replaced by interceptor_name, exported from |  | 
|   30   // interceptor_module. interceptor_entry_point can be provided instead of |  | 
|   31   // interceptor_name / interceptor_module. |  | 
|   32   // thunk_storage must point to a buffer on the child's address space, to hold |  | 
|   33   // the patch thunk, and related data. If provided, storage_used will receive |  | 
|   34   // the number of bytes used from thunk_storage. |  | 
|   35   // |  | 
|   36   // Example: (without error checking) |  | 
|   37   // |  | 
|   38   // size_t size = resolver.GetThunkSize(); |  | 
|   39   // char* buffer = ::VirtualAllocEx(child_process, NULL, size, |  | 
|   40   //                                 MEM_COMMIT, PAGE_READWRITE); |  | 
|   41   // resolver.Setup(ntdll_module, NULL, L"NtCreateFile", NULL, |  | 
|   42   //                &MyReplacementFunction, buffer, size, NULL); |  | 
|   43   // |  | 
|   44   // In general, the idea is to allocate a single big buffer for all |  | 
|   45   // interceptions on the same dll, and call Setup n times. |  | 
|   46   // WARNING: This means that any data member that is specific to a single |  | 
|   47   // interception must be reset within this method. |  | 
|   48   virtual NTSTATUS Setup(const void* target_module, |  | 
|   49                          const void* interceptor_module, |  | 
|   50                          const char* target_name, |  | 
|   51                          const char* interceptor_name, |  | 
|   52                          const void* interceptor_entry_point, |  | 
|   53                          void* thunk_storage, |  | 
|   54                          size_t storage_bytes, |  | 
|   55                          size_t* storage_used) = 0; |  | 
|   56  |  | 
|   57   // Gets the address of function_name inside module (main exe). |  | 
|   58   virtual NTSTATUS ResolveInterceptor(const void* module, |  | 
|   59                                       const char* function_name, |  | 
|   60                                       const void** address); |  | 
|   61  |  | 
|   62   // Gets the address of an exported function_name inside module. |  | 
|   63   virtual NTSTATUS ResolveTarget(const void* module, |  | 
|   64                                  const char* function_name, |  | 
|   65                                  void** address); |  | 
|   66  |  | 
|   67   // Gets the required buffer size for this type of thunk. |  | 
|   68   virtual size_t GetThunkSize() const = 0; |  | 
|   69  |  | 
|   70  protected: |  | 
|   71   // Performs basic initialization on behalf of a concrete instance of a |  | 
|   72   // resolver. That is, parameter validation and resolution of the target |  | 
|   73   // and the interceptor into the member variables. |  | 
|   74   // |  | 
|   75   // target_name is an exported function from the module loaded at |  | 
|   76   // target_module, and must be replaced by interceptor_name, exported from |  | 
|   77   // interceptor_module. interceptor_entry_point can be provided instead of |  | 
|   78   // interceptor_name / interceptor_module. |  | 
|   79   // thunk_storage must point to a buffer on the child's address space, to hold |  | 
|   80   // the patch thunk, and related data. |  | 
|   81   virtual NTSTATUS Init(const void* target_module, |  | 
|   82                         const void* interceptor_module, |  | 
|   83                         const char* target_name, |  | 
|   84                         const char* interceptor_name, |  | 
|   85                         const void* interceptor_entry_point, |  | 
|   86                         void* thunk_storage, |  | 
|   87                         size_t storage_bytes); |  | 
|   88  |  | 
|   89   // Gets the required buffer size for the internal part of the thunk. |  | 
|   90   size_t GetInternalThunkSize() const; |  | 
|   91  |  | 
|   92   // Initializes the internal part of the thunk. |  | 
|   93   // interceptor is the function to be called instead of original_function. |  | 
|   94   bool SetInternalThunk(void* storage, size_t storage_bytes, |  | 
|   95                         const void* original_function, const void* interceptor); |  | 
|   96  |  | 
|   97   // Holds the resolved interception target. |  | 
|   98   void* target_; |  | 
|   99   // Holds the resolved interception interceptor. |  | 
|  100   const void* interceptor_; |  | 
|  101  |  | 
|  102   DISALLOW_COPY_AND_ASSIGN(ResolverThunk); |  | 
|  103 }; |  | 
|  104  |  | 
|  105 }  // namespace sandbox |  | 
|  106  |  | 
|  107 #endif  // SANDBOX_SRC_RESOLVER_H__ |  | 
| OLD | NEW |