Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(570)

Side by Side Diff: handler/win/registration_pipe_state.cc

Issue 1126783004: Introduce RegistrationServer. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Review feedback. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "handler/win/registration_pipe_state.h"
16
17 #include <string.h>
18
19 #include <vector>
20
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "util/stdlib/pointer_container.h"
24
25 namespace crashpad {
26
27 RegistrationPipeState::RegistrationPipeState(
28 ScopedFileHANDLE pipe,
29 RegistrationServer::Delegate* delegate)
30 : request_(),
31 response_(),
32 completion_handler_(nullptr),
33 overlapped_(),
34 event_(),
35 pipe_(pipe.Pass()),
36 waiting_for_close_(false),
37 delegate_(delegate),
38 get_named_pipe_client_process_id_proc_(nullptr) {
39 HMODULE kernel_dll = GetModuleHandle(L"kernel32.dll");
40 if (kernel_dll) {
41 get_named_pipe_client_process_id_proc_ =
42 reinterpret_cast<decltype(GetNamedPipeClientProcessId)*>(
43 GetProcAddress(kernel_dll, "GetNamedPipeClientProcessId"));
44 }
45 }
46
47 RegistrationPipeState::~RegistrationPipeState() {
48 }
49
50 bool RegistrationPipeState::Initialize() {
51 DCHECK(!event_.is_valid());
52 DCHECK(pipe_.is_valid());
53
54 event_.reset(CreateEvent(nullptr, true, false, nullptr));
55
56 if (!event_.is_valid()) {
57 PLOG(ERROR) << "CreateEvent";
58 } else {
59 overlapped_.hEvent = event_.get();
60 if (IssueConnect())
61 return true;
62 }
63
64 overlapped_.hEvent = nullptr;
65 event_.reset();
66 pipe_.reset();
67 completion_handler_ = nullptr;
68
69 return false;
70 }
71
72 void RegistrationPipeState::Stop() {
73 DCHECK(pipe_.is_valid());
74 if (!CancelIo(pipe_.get()))
75 PLOG(FATAL) << "CancelIo";
76 }
77
78 bool RegistrationPipeState::OnCompletion() {
79 AsyncCompletionHandler completion_handler = completion_handler_;
80 completion_handler_ = nullptr;
81
82 DWORD bytes_transferred = 0;
83 BOOL success = GetOverlappedResult(pipe_.get(),
84 &overlapped_,
85 &bytes_transferred,
86 false); // Do not wait.
87 if (!success) {
88 // ERROR_BROKEN_PIPE is expected when we are waiting for the client to close
89 // the pipe (signaling that they are done reading the response).
90 if (!waiting_for_close_ || GetLastError() != ERROR_BROKEN_PIPE)
91 PLOG(ERROR) << "GetOverlappedResult";
92 }
93
94 bool still_running = false;
95 if (!ResetEvent(event_.get())) {
96 PLOG(ERROR) << "ResetEvent";
97 } else if (!completion_handler) {
98 NOTREACHED();
99 still_running = ResetConnection();
100 } else if (!success) {
101 still_running = ResetConnection();
102 } else {
103 still_running = (this->*completion_handler)(bytes_transferred);
104 }
105
106 if (!still_running) {
107 overlapped_.hEvent = nullptr;
108 event_.reset();
109 pipe_.reset();
110 completion_handler_ = nullptr;
111 } else {
112 DCHECK(completion_handler_);
113 }
114
115 return still_running;
116 }
117
118 bool RegistrationPipeState::OnConnectComplete(DWORD /* bytes_transferred */) {
119 return IssueRead();
120 }
121
122 bool RegistrationPipeState::OnReadComplete(DWORD bytes_transferred) {
123 if (bytes_transferred != sizeof(request_)) {
124 LOG(ERROR) << "Invalid message size: " << bytes_transferred;
125 return ResetConnection();
126 } else {
127 return HandleRequest();
128 }
129 }
130
131 bool RegistrationPipeState::OnWriteComplete(DWORD bytes_transferred) {
132 if (bytes_transferred != sizeof(response_)) {
133 LOG(ERROR) << "Incomplete write operation. Bytes written: "
134 << bytes_transferred;
135 }
136
137 return IssueWaitForClientClose();
138 }
139
140 bool RegistrationPipeState::OnWaitForClientCloseComplete(
141 DWORD bytes_transferred) {
142 LOG(ERROR) << "Unexpected extra data (" << bytes_transferred
143 << " bytes) received from client.";
144 return ResetConnection();
145 }
146
147 bool RegistrationPipeState::IssueConnect() {
148 if (ConnectNamedPipe(pipe_.get(), &overlapped_)) {
149 return OnConnectComplete(0); // bytes_transferred (ignored)
150 } else {
151 DWORD result = GetLastError();
152 if (result == ERROR_PIPE_CONNECTED) {
153 return OnConnectComplete(0); // bytes_transferred (ignored)
154 } else if (result == ERROR_IO_PENDING) {
155 completion_handler_ = &RegistrationPipeState::OnConnectComplete;
156 return true;
157 } else {
158 PLOG(ERROR) << "ConnectNamedPipe";
159 return false;
160 }
161 }
162 }
163
164 bool RegistrationPipeState::IssueRead() {
165 DWORD bytes_read = 0;
166 if (ReadFile(pipe_.get(),
167 &request_,
168 sizeof(request_),
169 &bytes_read,
170 &overlapped_)) {
171 return OnReadComplete(bytes_read);
172 } else if (GetLastError() == ERROR_IO_PENDING) {
173 completion_handler_ = &RegistrationPipeState::OnReadComplete;
174 return true;
175 } else {
176 PLOG(ERROR) << "ReadFile";
177 return ResetConnection();
178 }
179 }
180
181 bool RegistrationPipeState::IssueWrite() {
182 DWORD bytes_written = 0;
183 if (WriteFile(pipe_.get(),
184 &response_,
185 sizeof(response_),
186 &bytes_written,
187 &overlapped_)) {
188 return OnWriteComplete(bytes_written);
189 } else if (GetLastError() == ERROR_IO_PENDING) {
190 completion_handler_ = &RegistrationPipeState::OnWriteComplete;
191 return true;
192 } else {
193 PLOG(ERROR) << "WriteFile";
194 return ResetConnection();
195 }
196 }
197
198 bool RegistrationPipeState::IssueWaitForClientClose() {
199 waiting_for_close_ = true;
200 DWORD bytes_read = 0;
201 if (ReadFile(pipe_.get(),
202 &request_,
203 sizeof(request_),
204 &bytes_read,
205 &overlapped_)) {
206 return OnWaitForClientCloseComplete(bytes_read);
207 } else if (GetLastError() == ERROR_IO_PENDING) {
208 completion_handler_ = &RegistrationPipeState::OnWaitForClientCloseComplete;
209 return true;
210 } else {
211 PLOG(ERROR) << "ReadFile";
212 return ResetConnection();
213 }
214 }
215
216 bool RegistrationPipeState::HandleRequest() {
217 if (get_named_pipe_client_process_id_proc_) {
218 // On Vista+ we can verify that the client is who they claim to be, thus
219 // preventing arbitrary processes from having us duplicate handles into
220 // other processes.
221 DWORD real_client_process_id = 0;
222 if (!get_named_pipe_client_process_id_proc_(pipe_.get(),
223 &real_client_process_id)) {
224 PLOG(ERROR) << "GetNamedPipeClientProcessId";
225 } else if (real_client_process_id != request_.client_process_id) {
226 LOG(ERROR) << "Client process ID from request ("
227 << request_.client_process_id
228 << ") does not match pipe client process ID ("
229 << real_client_process_id << ").";
230 return ResetConnection();
231 }
232 }
233
234 ScopedKernelHANDLE client_process(
235 OpenProcess(PROCESS_ALL_ACCESS, false, request_.client_process_id));
236 if (!client_process.is_valid()) {
237 if (ImpersonateNamedPipeClient(pipe_.get())) {
238 client_process.reset(
239 OpenProcess(PROCESS_ALL_ACCESS, false, request_.client_process_id));
240 RevertToSelf();
241 }
242 }
243
244 if (!client_process.is_valid()) {
245 LOG(ERROR) << "Failed to open client process.";
246 return ResetConnection();
247 }
248
249 memset(&response_, 0, sizeof(response_));
250
251 HANDLE request_report_event = nullptr;
252 HANDLE report_complete_event = nullptr;
253
254 if (!delegate_->RegisterClient(client_process.Pass(),
255 request_.crashpad_info_address,
256 &request_report_event,
257 &report_complete_event)) {
258 return ResetConnection();
259 }
260
261 // A handle has at most 32 significant bits, though its type is void*. Thus we
262 // truncate it here. An interesting exception is INVALID_HANDLE_VALUE, which
263 // is '-1'. It is still safe to truncate it from 0xFFFFFFFFFFFFFFFF to
264 // 0xFFFFFFFF, but a 64-bit client receiving that value must correctly sign
265 // extend it.
266 response_.request_report_event =
267 reinterpret_cast<uint32_t>(request_report_event);
268 response_.report_complete_event =
269 reinterpret_cast<uint32_t>(report_complete_event);
270 return IssueWrite();
271 }
272
273 bool RegistrationPipeState::ResetConnection() {
274 memset(&request_, 0, sizeof(request_));
275 waiting_for_close_ = false;
276
277 if (!DisconnectNamedPipe(pipe_.get())) {
278 PLOG(ERROR) << "DisconnectNamedPipe";
279 return false;
280 } else {
281 return IssueConnect();
282 }
283 }
284
285 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698