| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/win/exception_handler_server.h" | 15 #include "util/win/exception_handler_server.h" |
| 16 | 16 |
| 17 #include <sddl.h> |
| 17 #include <string.h> | 18 #include <string.h> |
| 18 | 19 |
| 19 #include "base/logging.h" | 20 #include "base/logging.h" |
| 20 #include "base/numerics/safe_conversions.h" | 21 #include "base/numerics/safe_conversions.h" |
| 21 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 22 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 24 #include "minidump/minidump_file_writer.h" | 25 #include "minidump/minidump_file_writer.h" |
| 25 #include "snapshot/crashpad_info_client_options.h" | 26 #include "snapshot/crashpad_info_client_options.h" |
| 26 #include "snapshot/win/process_snapshot_win.h" | 27 #include "snapshot/win/process_snapshot_win.h" |
| 27 #include "util/file/file_writer.h" | 28 #include "util/file/file_writer.h" |
| 28 #include "util/misc/tri_state.h" | 29 #include "util/misc/tri_state.h" |
| 29 #include "util/misc/uuid.h" | 30 #include "util/misc/uuid.h" |
| 30 #include "util/win/get_function.h" | 31 #include "util/win/get_function.h" |
| 31 #include "util/win/handle.h" | 32 #include "util/win/handle.h" |
| 32 #include "util/win/registration_protocol_win.h" | 33 #include "util/win/registration_protocol_win.h" |
| 34 #include "util/win/scoped_local_alloc.h" |
| 33 #include "util/win/xp_compat.h" | 35 #include "util/win/xp_compat.h" |
| 34 | 36 |
| 35 namespace crashpad { | 37 namespace crashpad { |
| 36 | 38 |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 39 // We create two pipe instances, so that there's one listening while the | 41 // We create two pipe instances, so that there's one listening while the |
| 40 // PipeServiceProc is processing a registration. | 42 // PipeServiceProc is processing a registration. |
| 41 const size_t kPipeInstances = 2; | 43 const size_t kPipeInstances = 2; |
| 42 | 44 |
| 43 // Wraps CreateNamedPipe() to create a single named pipe instance. | 45 // Wraps CreateNamedPipe() to create a single named pipe instance. |
| 44 // | 46 // |
| 45 // If first_instance is true, the named pipe instance will be created with | 47 // If first_instance is true, the named pipe instance will be created with |
| 46 // FILE_FLAG_FIRST_PIPE_INSTANCE. This ensures that the the pipe name is not | 48 // FILE_FLAG_FIRST_PIPE_INSTANCE. This ensures that the the pipe name is not |
| 47 // already in use when created. | 49 // already in use when created. The first instance will be created with an |
| 50 // untrusted integrity SACL so instances of this pipe can be connected to by |
| 51 // processes of any integrity level. |
| 48 HANDLE CreateNamedPipeInstance(const std::wstring& pipe_name, | 52 HANDLE CreateNamedPipeInstance(const std::wstring& pipe_name, |
| 49 bool first_instance) { | 53 bool first_instance) { |
| 50 return CreateNamedPipe(pipe_name.c_str(), | 54 SECURITY_ATTRIBUTES security_attributes; |
| 51 PIPE_ACCESS_DUPLEX | | 55 SECURITY_ATTRIBUTES* security_attributes_pointer = nullptr; |
| 52 (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE | 56 ScopedLocalAlloc scoped_sec_desc; |
| 53 : 0), | 57 |
| 54 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, | 58 if (first_instance) { |
| 55 kPipeInstances, | 59 // Pre-Vista does not have integrity levels. |
| 56 512, | 60 const DWORD version = GetVersion(); |
| 57 512, | 61 const DWORD major_version = LOBYTE(LOWORD(version)); |
| 58 0, | 62 const bool is_vista_or_later = major_version >= 6; |
| 59 nullptr); | 63 if (is_vista_or_later) { |
| 64 // Mandatory Label, no ACE flags, no ObjectType, integrity level |
| 65 // untrusted. |
| 66 const wchar_t kSddl[] = L"S:(ML;;;;;S-1-16-0)"; |
| 67 |
| 68 PSECURITY_DESCRIPTOR sec_desc; |
| 69 PCHECK(ConvertStringSecurityDescriptorToSecurityDescriptor( |
| 70 kSddl, SDDL_REVISION_1, &sec_desc, nullptr)) |
| 71 << "ConvertStringSecurityDescriptorToSecurityDescriptor"; |
| 72 |
| 73 // Take ownership of the allocated SECURITY_DESCRIPTOR. |
| 74 scoped_sec_desc.reset(sec_desc); |
| 75 |
| 76 memset(&security_attributes, 0, sizeof(security_attributes)); |
| 77 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 78 security_attributes.lpSecurityDescriptor = sec_desc; |
| 79 security_attributes.bInheritHandle = FALSE; |
| 80 security_attributes_pointer = &security_attributes; |
| 81 } |
| 82 } |
| 83 |
| 84 return CreateNamedPipe( |
| 85 pipe_name.c_str(), |
| 86 PIPE_ACCESS_DUPLEX | (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0), |
| 87 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, |
| 88 kPipeInstances, |
| 89 512, |
| 90 512, |
| 91 0, |
| 92 security_attributes_pointer); |
| 60 } | 93 } |
| 61 | 94 |
| 62 decltype(GetNamedPipeClientProcessId)* GetNamedPipeClientProcessIdFunction() { | 95 decltype(GetNamedPipeClientProcessId)* GetNamedPipeClientProcessIdFunction() { |
| 63 static const auto get_named_pipe_client_process_id = | 96 static const auto get_named_pipe_client_process_id = |
| 64 GET_FUNCTION(L"kernel32.dll", ::GetNamedPipeClientProcessId); | 97 GET_FUNCTION(L"kernel32.dll", ::GetNamedPipeClientProcessId); |
| 65 return get_named_pipe_client_process_id; | 98 return get_named_pipe_client_process_id; |
| 66 } | 99 } |
| 67 | 100 |
| 68 HANDLE DuplicateEvent(HANDLE process, HANDLE event) { | 101 HANDLE DuplicateEvent(HANDLE process, HANDLE event) { |
| 69 HANDLE handle; | 102 HANDLE handle; |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 } | 346 } |
| 314 | 347 |
| 315 void ExceptionHandlerServer::Run(Delegate* delegate) { | 348 void ExceptionHandlerServer::Run(Delegate* delegate) { |
| 316 uint64_t shutdown_token = base::RandUint64(); | 349 uint64_t shutdown_token = base::RandUint64(); |
| 317 ScopedKernelHANDLE thread_handles[kPipeInstances]; | 350 ScopedKernelHANDLE thread_handles[kPipeInstances]; |
| 318 for (int i = 0; i < arraysize(thread_handles); ++i) { | 351 for (int i = 0; i < arraysize(thread_handles); ++i) { |
| 319 HANDLE pipe; | 352 HANDLE pipe; |
| 320 if (first_pipe_instance_.is_valid()) { | 353 if (first_pipe_instance_.is_valid()) { |
| 321 pipe = first_pipe_instance_.release(); | 354 pipe = first_pipe_instance_.release(); |
| 322 } else { | 355 } else { |
| 323 pipe = CreateNamedPipeInstance(pipe_name_, false); | 356 pipe = CreateNamedPipeInstance(pipe_name_, i == 0); |
| 324 PCHECK(pipe != INVALID_HANDLE_VALUE) << "CreateNamedPipe"; | 357 PCHECK(pipe != INVALID_HANDLE_VALUE) << "CreateNamedPipe"; |
| 325 } | 358 } |
| 326 | 359 |
| 327 // Ownership of this object (and the pipe instance) is given to the new | 360 // Ownership of this object (and the pipe instance) is given to the new |
| 328 // thread. We close the thread handles at the end of the scope. They clean | 361 // thread. We close the thread handles at the end of the scope. They clean |
| 329 // up the context object and the pipe instance on termination. | 362 // up the context object and the pipe instance on termination. |
| 330 internal::PipeServiceContext* context = | 363 internal::PipeServiceContext* context = |
| 331 new internal::PipeServiceContext(port_.get(), | 364 new internal::PipeServiceContext(port_.get(), |
| 332 pipe, | 365 pipe, |
| 333 delegate, | 366 delegate, |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 void __stdcall ExceptionHandlerServer::OnProcessEnd(void* ctx, BOOLEAN) { | 594 void __stdcall ExceptionHandlerServer::OnProcessEnd(void* ctx, BOOLEAN) { |
| 562 // This function is executed on the thread pool. | 595 // This function is executed on the thread pool. |
| 563 internal::ClientData* client = reinterpret_cast<internal::ClientData*>(ctx); | 596 internal::ClientData* client = reinterpret_cast<internal::ClientData*>(ctx); |
| 564 base::AutoLock lock(*client->lock()); | 597 base::AutoLock lock(*client->lock()); |
| 565 | 598 |
| 566 // Post back to the main thread to have it delete this client record. | 599 // Post back to the main thread to have it delete this client record. |
| 567 PostQueuedCompletionStatus(client->port(), 0, ULONG_PTR(client), nullptr); | 600 PostQueuedCompletionStatus(client->port(), 0, ULONG_PTR(client), nullptr); |
| 568 } | 601 } |
| 569 | 602 |
| 570 } // namespace crashpad | 603 } // namespace crashpad |
| OLD | NEW |