File: module_event_sink_impl_win.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (211 lines) | stat: -rw-r--r-- 7,584 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/conflicts/module_event_sink_impl_win.h"

#include <windows.h>
#include <psapi.h>

#include <utility>

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_piece.h"
#include "chrome/browser/conflicts/module_database_win.h"
#include "chrome/common/conflicts/module_watcher_win.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/strong_binding.h"

namespace {

// Gets the path of the module in the provided remote process. Returns true on
// success, false otherwise.
bool GetModulePath(base::ProcessHandle process,
                   HMODULE module,
                   base::FilePath* path) {
  std::vector<wchar_t> temp_path(MAX_PATH);
  size_t length = 0;
  while (true) {
    length = ::GetModuleFileNameEx(process, module, temp_path.data(),
                                   temp_path.size());
    if (length == 0)
      return false;
    if (length < temp_path.size())
      break;
    // The entire buffer was consumed, so grow it to ensure the result wasn't
    // actually truncated.
    temp_path.resize(2 * temp_path.size());
  }

  *path = base::FilePath(base::StringPiece16(temp_path.data(), length));
  return true;
}

// Gets the size of a module in a remote process. Returns true on success, false
// otherwise.
bool GetModuleSize(base::ProcessHandle process,
                   HMODULE module,
                   uint32_t* size) {
  MODULEINFO info = {};
  if (!::GetModuleInformation(process, module, &info, sizeof(info)))
    return false;
  *size = info.SizeOfImage;
  return true;
}

// Reads the typed data from a remote process. Returns true on success, false
// otherwise.
template <typename T>
bool ReadRemoteData(base::ProcessHandle process, uint64_t address, T* data) {
  const void* typed_address =
      reinterpret_cast<const void*>(static_cast<uintptr_t>(address));
  SIZE_T bytes_read = 0;
  if (!::ReadProcessMemory(process, typed_address, &data, sizeof(data),
                           &bytes_read)) {
    return false;
  }
  if (bytes_read != sizeof(data))
    return false;
  return true;
}

// Reads the time date stamp from the module loaded in the provided remote
// |process| at the provided remote |load_address|.
bool GetModuleTimeDateStamp(base::ProcessHandle process,
                            uint64_t load_address,
                            uint32_t* time_date_stamp) {
  uint64_t address = load_address + offsetof(IMAGE_DOS_HEADER, e_lfanew);
  LONG e_lfanew = 0;
  if (!ReadRemoteData(process, address, &e_lfanew))
    return false;

  address = load_address + e_lfanew + offsetof(IMAGE_NT_HEADERS, FileHeader) +
            offsetof(IMAGE_FILE_HEADER, TimeDateStamp);
  DWORD temp = 0;
  if (!ReadRemoteData(process, address, &temp))
    return false;

  *time_date_stamp = temp;
  return true;
}

}  // namespace

ModuleEventSinkImpl::ModuleEventSinkImpl(base::ProcessHandle process,
                                         content::ProcessType process_type,
                                         ModuleDatabase* module_database)
    : process_(process),
      module_database_(module_database),
      process_id_(0),
      creation_time_(0),
      in_error_(false) {
  // Failing to get basic process information means this channel should not
  // continue to forward data, thus it is marked as being in error.
  process_id_ = ::GetProcessId(process_);
  if (!GetProcessCreationTime(process_, &creation_time_)) {
    in_error_ = true;
    return;
  }
  module_database->OnProcessStarted(process_id_, creation_time_, process_type);
}

ModuleEventSinkImpl::~ModuleEventSinkImpl() = default;

// static
void ModuleEventSinkImpl::Create(GetProcessHandleCallback get_process_handle,
                                 content::ProcessType process_type,
                                 ModuleDatabase* module_database,
                                 mojom::ModuleEventSinkRequest request) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  base::ProcessHandle process = get_process_handle.Run();
  auto module_event_sink_impl = base::MakeUnique<ModuleEventSinkImpl>(
      process, process_type, module_database);
  base::Closure error_handler = base::Bind(
      &ModuleDatabase::OnProcessEnded, base::Unretained(module_database),
      module_event_sink_impl->process_id_,
      module_event_sink_impl->creation_time_);
  auto binding = mojo::MakeStrongBinding(std::move(module_event_sink_impl),
                                         std::move(request));
  binding->set_connection_error_handler(error_handler);
}

void ModuleEventSinkImpl::OnModuleEvent(mojom::ModuleEventType event_type,
                                        uint64_t load_address) {
  if (in_error_)
    return;

  // Mojo takes care of validating |event_type|, so only |load_address| needs to
  // be checked. Load addresses must be aligned with the allocation granularity
  // which is at least 64KB on any supported Windows OS.
  if (load_address == 0 || load_address % (64 * 1024) != 0)
    return;

  switch (event_type) {
    case mojom::ModuleEventType::MODULE_ALREADY_LOADED:
    case mojom::ModuleEventType::MODULE_LOADED:
      return OnModuleLoad(load_address);

    case mojom::ModuleEventType::MODULE_UNLOADED:
      return OnModuleUnload(load_address);
  }
}

// static
bool ModuleEventSinkImpl::GetProcessCreationTime(base::ProcessHandle process,
                                                 uint64_t* creation_time) {
  FILETIME creation_ft = {};
  FILETIME exit_ft = {};
  FILETIME kernel_ft = {};
  FILETIME user_ft = {};
  if (!::GetProcessTimes(process, &creation_ft, &exit_ft, &kernel_ft,
                         &user_ft)) {
    return false;
  }
  *creation_time = (static_cast<uint64_t>(creation_ft.dwHighDateTime) << 32) |
                   static_cast<uint64_t>(creation_ft.dwLowDateTime);
  return true;
}

void ModuleEventSinkImpl::OnModuleLoad(uint64_t load_address) {
  if (in_error_)
    return;

  // The |load_address| is a unique key to a module in a remote process. If
  // there is a valid module there then the following queries should all pass.
  // If any of them fail then the load event is silently swallowed. The entire
  // channel is not marked as being in an error mode, as later events may be
  // well formed.

  // Convert the |load_address| to a module handle.
  HMODULE module =
      reinterpret_cast<HMODULE>(static_cast<uintptr_t>(load_address));

  // Look up the various pieces of module metadata in the remote process.

  base::FilePath module_path;
  if (!GetModulePath(process_, module, &module_path))
    return;

  uint32_t module_size = 0;
  if (!GetModuleSize(process_, module, &module_size))
    return;

  uint32_t module_time_date_stamp = 0;
  if (!GetModuleTimeDateStamp(process_, load_address, &module_time_date_stamp))
    return;

  // Forward this to the module database.
  module_database_->OnModuleLoad(process_id_, creation_time_, module_path,
                                 module_size, module_time_date_stamp,
                                 load_address);
}

void ModuleEventSinkImpl::OnModuleUnload(uint64_t load_address) {
  // Forward this directly to the module database.
  module_database_->OnModuleUnload(process_id_, creation_time_,
                                   static_cast<uintptr_t>(load_address));
}