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

Side by Side Diff: util/win/exception_handler_server.cc

Issue 1405093013: win: Lower integrity level of connection pipe (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: set first_instance when specifying a pipe name Created 5 years, 1 month 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
« no previous file with comments | « util/util.gyp ('k') | util/win/scoped_local_free.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <aclapi.h>
18 #include <sddl.h>
17 #include <string.h> 19 #include <string.h>
18 20
19 #include "base/logging.h" 21 #include "base/logging.h"
20 #include "base/numerics/safe_conversions.h" 22 #include "base/numerics/safe_conversions.h"
21 #include "base/rand_util.h" 23 #include "base/rand_util.h"
22 #include "base/strings/stringprintf.h" 24 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
24 #include "minidump/minidump_file_writer.h" 26 #include "minidump/minidump_file_writer.h"
25 #include "snapshot/crashpad_info_client_options.h" 27 #include "snapshot/crashpad_info_client_options.h"
26 #include "snapshot/win/process_snapshot_win.h" 28 #include "snapshot/win/process_snapshot_win.h"
27 #include "util/file/file_writer.h" 29 #include "util/file/file_writer.h"
28 #include "util/misc/tri_state.h" 30 #include "util/misc/tri_state.h"
29 #include "util/misc/uuid.h" 31 #include "util/misc/uuid.h"
30 #include "util/win/get_function.h" 32 #include "util/win/get_function.h"
31 #include "util/win/handle.h" 33 #include "util/win/handle.h"
32 #include "util/win/registration_protocol_win.h" 34 #include "util/win/registration_protocol_win.h"
35 #include "util/win/scoped_local_free.h"
33 #include "util/win/xp_compat.h" 36 #include "util/win/xp_compat.h"
34 37
35 namespace crashpad { 38 namespace crashpad {
36 39
37 namespace { 40 namespace {
38 41
39 // We create two pipe instances, so that there's one listening while the 42 // We create two pipe instances, so that there's one listening while the
40 // PipeServiceProc is processing a registration. 43 // PipeServiceProc is processing a registration.
41 const size_t kPipeInstances = 2; 44 const size_t kPipeInstances = 2;
42 45
43 // Wraps CreateNamedPipe() to create a single named pipe instance. 46 // Wraps CreateNamedPipe() to create a single named pipe instance.
44 // 47 //
45 // If first_instance is true, the named pipe instance will be created with 48 // 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 49 // FILE_FLAG_FIRST_PIPE_INSTANCE. This ensures that the the pipe name is not
47 // already in use when created. 50 // already in use when created.
51 //
52 // The integrity level of the pipe is lowered so that it can be connected to by
53 // processes at any integrity level.
48 HANDLE CreateNamedPipeInstance(const std::wstring& pipe_name, 54 HANDLE CreateNamedPipeInstance(const std::wstring& pipe_name,
49 bool first_instance) { 55 bool first_instance) {
50 return CreateNamedPipe(pipe_name.c_str(), 56 ScopedFileHandle pipe(CreateNamedPipe(
51 PIPE_ACCESS_DUPLEX | 57 pipe_name.c_str(),
52 (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE 58 PIPE_ACCESS_DUPLEX | (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0),
53 : 0), 59 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
54 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 60 kPipeInstances,
55 kPipeInstances, 61 512,
56 512, 62 512,
57 512, 63 0,
58 0, 64 nullptr));
59 nullptr); 65 if (!pipe.is_valid()) {
66 PLOG(ERROR) << "CreateNamedPipe";
67 return INVALID_HANDLE_VALUE;
68 }
69
70 // We only need to set the integrity level on the first instance of the pipe.
71 if (!first_instance)
72 return pipe.release();
73
74 // Lower the integrity of the pipe so that it can be connected to from
75 // processes at any integrity level. This only applies to Vista and later.
76 const DWORD version = GetVersion();
77 const DWORD major_version = LOBYTE(LOWORD(version));
78 const bool is_pre_vista = major_version < 6;
79 if (is_pre_vista)
80 return pipe.release();
81
82 // Mandatory Label, no ACE flags, no ObjectType, integrity level untrusted.
83 const wchar_t kSddl[] = L"S:(ML;;;;;S-1-16-0)";
84
85 SECURITY_DESCRIPTOR* sec_desc = nullptr;
86
87 PACL* sacl = nullptr;
Mark Mentovai 2015/11/05 21:36:15 ACL**?
scottmg 2015/11/05 22:29:59 Oops. Sadly, now that I try, um, compiling, ACL*
88 if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
89 kSddl, SDDL_REVISION, &sec_desc, nullptr)) {
Mark Mentovai 2015/11/05 21:36:15 Be explicit about the format of the string you’re
scottmg 2015/11/05 22:29:59 Done.
90 PLOG(ERROR) << "ConvertStringSecurityDescriptorToSecurityDescriptor";
91 return INVALID_HANDLE_VALUE;
92 }
93
94 // Take ownership of the allocated SECURITY_DESCRIPTOR.
95 ScopedLocalFree scoped_sec_desc(sec_desc);
96
97 BOOL sacl_present = FALSE;
98 BOOL sacl_defaulted = FALSE;
99 if (!GetSecurityDescriptorSacl(
100 sec_desc, &sacl_present, &sacl, &sacl_defaulted)) {
Mark Mentovai 2015/11/05 21:36:15 sacl points to something in sec_desc, so no need t
scottmg 2015/11/05 22:29:59 Yes, that's my understanding from the docs. Maybe
101 PLOG(ERROR) << "GetSecurityDescriptorSacl";
102 return INVALID_HANDLE_VALUE;
103 }
104
105 DWORD error = SetSecurityInfo(pipe.get(),
106 SE_KERNEL_OBJECT,
107 LABEL_SECURITY_INFORMATION,
108 nullptr,
109 nullptr,
110 nullptr,
111 sacl);
112 if (error != ERROR_SUCCESS) {
113 LOG(ERROR) << "SetSecurityInfo: "
114 << logging::SystemErrorCodeToString(error);
115 return INVALID_HANDLE_VALUE;
116 }
117
118 return pipe.release();
60 } 119 }
61 120
62 decltype(GetNamedPipeClientProcessId)* GetNamedPipeClientProcessIdFunction() { 121 decltype(GetNamedPipeClientProcessId)* GetNamedPipeClientProcessIdFunction() {
63 static const auto get_named_pipe_client_process_id = 122 static const auto get_named_pipe_client_process_id =
64 GET_FUNCTION(L"kernel32.dll", ::GetNamedPipeClientProcessId); 123 GET_FUNCTION(L"kernel32.dll", ::GetNamedPipeClientProcessId);
65 return get_named_pipe_client_process_id; 124 return get_named_pipe_client_process_id;
66 } 125 }
67 126
68 HANDLE DuplicateEvent(HANDLE process, HANDLE event) { 127 HANDLE DuplicateEvent(HANDLE process, HANDLE event) {
69 HANDLE handle; 128 HANDLE handle;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 int tries = 5; 342 int tries = 5;
284 std::string pipe_name_base = 343 std::string pipe_name_base =
285 base::StringPrintf("\\\\.\\pipe\\crashpad_%d_", GetCurrentProcessId()); 344 base::StringPrintf("\\\\.\\pipe\\crashpad_%d_", GetCurrentProcessId());
286 std::wstring pipe_name; 345 std::wstring pipe_name;
287 do { 346 do {
288 pipe_name = base::UTF8ToUTF16(pipe_name_base); 347 pipe_name = base::UTF8ToUTF16(pipe_name_base);
289 for (int index = 0; index < 16; ++index) { 348 for (int index = 0; index < 16; ++index) {
290 pipe_name.append(1, static_cast<wchar_t>(base::RandInt('A', 'Z'))); 349 pipe_name.append(1, static_cast<wchar_t>(base::RandInt('A', 'Z')));
291 } 350 }
292 351
293 first_pipe_instance_.reset(CreateNamedPipeInstance(pipe_name, true)); 352 first_pipe_instance_.reset(CreateNamedPipeInstance(pipe_name, true));
Mark Mentovai 2015/11/05 21:36:15 The logging needs to be rationalized a bit. Now yo
scottmg 2015/11/05 22:29:59 (no lowering function now)
scottmg 2015/11/05 22:29:59 I think we're back to previous logging behaviour n
294 353
295 // CreateNamedPipe() is documented as setting the error to 354 // CreateNamedPipe() is documented as setting the error to
296 // ERROR_ACCESS_DENIED if FILE_FLAG_FIRST_PIPE_INSTANCE is specified and the 355 // ERROR_ACCESS_DENIED if FILE_FLAG_FIRST_PIPE_INSTANCE is specified and the
297 // pipe name is already in use. However it may set the error to other codes 356 // pipe name is already in use. However it may set the error to other codes
298 // such as ERROR_PIPE_BUSY (if the pipe already exists and has reached its 357 // such as ERROR_PIPE_BUSY (if the pipe already exists and has reached its
299 // maximum instance count) or ERROR_INVALID_PARAMETER (if the pipe already 358 // maximum instance count) or ERROR_INVALID_PARAMETER (if the pipe already
300 // exists and its attributes differ from those specified to 359 // exists and its attributes differ from those specified to
301 // CreateNamedPipe()). Some of these errors may be ambiguous: for example, 360 // CreateNamedPipe()). Some of these errors may be ambiguous: for example,
302 // ERROR_INVALID_PARAMETER may also occur if CreateNamedPipe() is called 361 // ERROR_INVALID_PARAMETER may also occur if CreateNamedPipe() is called
303 // incorrectly even in the absence of an existing pipe by the same name. 362 // incorrectly even in the absence of an existing pipe by the same name.
304 // 363 //
305 // Rather than chasing down all of the possible errors that might indicate 364 // Rather than chasing down all of the possible errors that might indicate
306 // that a pipe name is already in use, retry up to a few times on any error. 365 // that a pipe name is already in use, retry up to a few times on any error.
307 } while (!first_pipe_instance_.is_valid() && --tries); 366 } while (!first_pipe_instance_.is_valid() && --tries);
308 367
309 PCHECK(first_pipe_instance_.is_valid()) << "CreateNamedPipe"; 368 PCHECK(first_pipe_instance_.is_valid()) << "CreateNamedPipe";
310 369
311 SetPipeName(pipe_name); 370 SetPipeName(pipe_name);
312 return pipe_name; 371 return pipe_name;
313 } 372 }
314 373
315 void ExceptionHandlerServer::Run(Delegate* delegate) { 374 void ExceptionHandlerServer::Run(Delegate* delegate) {
316 uint64_t shutdown_token = base::RandUint64(); 375 uint64_t shutdown_token = base::RandUint64();
317 ScopedKernelHANDLE thread_handles[kPipeInstances]; 376 ScopedKernelHANDLE thread_handles[kPipeInstances];
318 for (int i = 0; i < arraysize(thread_handles); ++i) { 377 for (int i = 0; i < arraysize(thread_handles); ++i) {
319 HANDLE pipe; 378 HANDLE pipe;
320 if (first_pipe_instance_.is_valid()) { 379 if (first_pipe_instance_.is_valid()) {
321 pipe = first_pipe_instance_.release(); 380 pipe = first_pipe_instance_.release();
322 } else { 381 } else {
323 pipe = CreateNamedPipeInstance(pipe_name_, false); 382 pipe = CreateNamedPipeInstance(pipe_name_, i == 0);
324 PCHECK(pipe != INVALID_HANDLE_VALUE) << "CreateNamedPipe"; 383 PCHECK(pipe != INVALID_HANDLE_VALUE) << "CreateNamedPipe";
325 } 384 }
326 385
Mark Mentovai 2015/11/05 21:39:11 Maybe this works out better from that perspective
scottmg 2015/11/05 22:29:59 Yeah, that seems better. Done. ... Hmm, it would
327 // Ownership of this object (and the pipe instance) is given to the new 386 // 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 387 // 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. 388 // up the context object and the pipe instance on termination.
330 internal::PipeServiceContext* context = 389 internal::PipeServiceContext* context =
331 new internal::PipeServiceContext(port_.get(), 390 new internal::PipeServiceContext(port_.get(),
332 pipe, 391 pipe,
333 delegate, 392 delegate,
334 &clients_lock_, 393 &clients_lock_,
335 &clients_, 394 &clients_,
336 shutdown_token); 395 shutdown_token);
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 void __stdcall ExceptionHandlerServer::OnProcessEnd(void* ctx, BOOLEAN) { 620 void __stdcall ExceptionHandlerServer::OnProcessEnd(void* ctx, BOOLEAN) {
562 // This function is executed on the thread pool. 621 // This function is executed on the thread pool.
563 internal::ClientData* client = reinterpret_cast<internal::ClientData*>(ctx); 622 internal::ClientData* client = reinterpret_cast<internal::ClientData*>(ctx);
564 base::AutoLock lock(*client->lock()); 623 base::AutoLock lock(*client->lock());
565 624
566 // Post back to the main thread to have it delete this client record. 625 // Post back to the main thread to have it delete this client record.
567 PostQueuedCompletionStatus(client->port(), 0, ULONG_PTR(client), nullptr); 626 PostQueuedCompletionStatus(client->port(), 0, ULONG_PTR(client), nullptr);
568 } 627 }
569 628
570 } // namespace crashpad 629 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/util.gyp ('k') | util/win/scoped_local_free.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698