michael@0: // Copyright (c) 2006-2008 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: // Defines InterceptionAgent, the class in charge of setting up interceptions michael@0: // from the inside of the sandboxed process. For more details see michael@0: // http://dev.chromium.org/developers/design-documents/sandbox . michael@0: michael@0: #ifndef SANDBOX_SRC_INTERCEPTION_AGENT_H__ michael@0: #define SANDBOX_SRC_INTERCEPTION_AGENT_H__ michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "sandbox/win/src/nt_internals.h" michael@0: #include "sandbox/win/src/sandbox_types.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: // Internal structures used for communication between the broker and the target. michael@0: struct DllInterceptionData; michael@0: struct SharedMemory; michael@0: struct DllPatchInfo; michael@0: michael@0: class ResolverThunk; michael@0: michael@0: // The InterceptionAgent executes on the target application, and it is in charge michael@0: // of setting up the desired interceptions or indicating what module needs to michael@0: // be unloaded. michael@0: // michael@0: // The exposed API consists of three methods: GetInterceptionAgent to retrieve michael@0: // the single class instance, OnDllLoad and OnDllUnload to process a dll being michael@0: // loaded and unloaded respectively. michael@0: // michael@0: // This class assumes that it will get called for every dll being loaded, michael@0: // starting with kernel32, so the singleton will be instantiated from within the michael@0: // loader lock. michael@0: class InterceptionAgent { michael@0: public: michael@0: // Returns the single InterceptionAgent object for this process. michael@0: static InterceptionAgent* GetInterceptionAgent(); michael@0: michael@0: // This method should be invoked whenever a new dll is loaded to perform the michael@0: // required patches. If the return value is false, this dll should not be michael@0: // allowed to load. michael@0: // michael@0: // full_path is the (optional) full name of the module being loaded and name michael@0: // is the internal module name. If full_path is provided, it will be used michael@0: // before the internal name to determine if we care about this dll. michael@0: bool OnDllLoad(const UNICODE_STRING* full_path, const UNICODE_STRING* name, michael@0: void* base_address); michael@0: michael@0: // Performs cleanup when a dll is unloaded. michael@0: void OnDllUnload(void* base_address); michael@0: michael@0: private: michael@0: ~InterceptionAgent() {} michael@0: michael@0: // Performs initialization of the singleton. michael@0: bool Init(SharedMemory* shared_memory); michael@0: michael@0: // Returns true if we are interested on this dll. dll_info is an entry of the michael@0: // list of intercepted dlls. michael@0: bool DllMatch(const UNICODE_STRING* full_path, const UNICODE_STRING* name, michael@0: const DllPatchInfo* dll_info); michael@0: michael@0: // Performs the patching of the dll loaded at base_address. michael@0: // The patches to perform are described on dll_info, and thunks is the thunk michael@0: // storage for the whole dll. michael@0: // Returns true on success. michael@0: bool PatchDll(const DllPatchInfo* dll_info, DllInterceptionData* thunks); michael@0: michael@0: // Returns a resolver for a given interception type. michael@0: ResolverThunk* GetResolver(InterceptionType type); michael@0: michael@0: // Shared memory containing the list of functions to intercept. michael@0: SharedMemory* interceptions_; michael@0: michael@0: // Array of thunk data buffers for the intercepted dlls. This object singleton michael@0: // is allocated with a placement new with enough space to hold the complete michael@0: // array of pointers, not just the first element. michael@0: DllInterceptionData* dlls_[1]; michael@0: michael@0: DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptionAgent); michael@0: }; michael@0: michael@0: } // namespace sandbox michael@0: michael@0: #endif // SANDBOX_SRC_INTERCEPTION_AGENT_H__