1// Copyright (c) 2006-2008 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 InterceptionAgent, the class in charge of setting up interceptions
6// from the inside of the sandboxed process. For more details see
7// http://dev.chromium.org/developers/design-documents/sandbox .
8
9#ifndef SANDBOX_SRC_INTERCEPTION_AGENT_H__
10#define SANDBOX_SRC_INTERCEPTION_AGENT_H__
11
12#include "base/basictypes.h"
13#include "sandbox/win/src/nt_internals.h"
14#include "sandbox/win/src/sandbox_types.h"
15
16namespace sandbox {
17
18// Internal structures used for communication between the broker and the target.
19struct DllInterceptionData;
20struct SharedMemory;
21struct DllPatchInfo;
22
23class ResolverThunk;
24
25// The InterceptionAgent executes on the target application, and it is in charge
26// of setting up the desired interceptions or indicating what module needs to
27// be unloaded.
28//
29// The exposed API consists of three methods: GetInterceptionAgent to retrieve
30// the single class instance, OnDllLoad and OnDllUnload to process a dll being
31// loaded and unloaded respectively.
32//
33// This class assumes that it will get called for every dll being loaded,
34// starting with kernel32, so the singleton will be instantiated from within the
35// loader lock.
36class InterceptionAgent {
37 public:
38  // Returns the single InterceptionAgent object for this process.
39  static InterceptionAgent* GetInterceptionAgent();
40
41  // This method should be invoked whenever a new dll is loaded to perform the
42  // required patches. If the return value is false, this dll should not be
43  // allowed to load.
44  //
45  // full_path is the (optional) full name of the module being loaded and name
46  // is the internal module name. If full_path is provided, it will be used
47  // before the internal name to determine if we care about this dll.
48  bool OnDllLoad(const UNICODE_STRING* full_path, const UNICODE_STRING* name,
49                 void* base_address);
50
51  // Performs cleanup when a dll is unloaded.
52  void OnDllUnload(void* base_address);
53
54 private:
55  ~InterceptionAgent() {}
56
57  // Performs initialization of the singleton.
58  bool Init(SharedMemory* shared_memory);
59
60  // Returns true if we are interested on this dll. dll_info is an entry of the
61  // list of intercepted dlls.
62  bool DllMatch(const UNICODE_STRING* full_path, const UNICODE_STRING* name,
63                const DllPatchInfo* dll_info);
64
65  // Performs the patching of the dll loaded at base_address.
66  // The patches to perform are described on dll_info, and thunks is the thunk
67  // storage for the whole dll.
68  // Returns true on success.
69  bool PatchDll(const DllPatchInfo* dll_info, DllInterceptionData* thunks);
70
71  // Returns a resolver for a given interception type.
72  ResolverThunk* GetResolver(InterceptionType type);
73
74  // Shared memory containing the list of functions to intercept.
75  SharedMemory* interceptions_;
76
77  // Array of thunk data buffers for the intercepted dlls. This object singleton
78  // is allocated with a placement new with enough space to hold the complete
79  // array of pointers, not just the first element.
80  DllInterceptionData* dlls_[1];
81
82  DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptionAgent);
83};
84
85}  // namespace sandbox
86
87#endif  // SANDBOX_SRC_INTERCEPTION_AGENT_H__
88