| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/security_key/remote_security_key_main.h" | 5 #include "remoting/host/security_key/remote_security_key_main.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "remoting/host/host_exit_codes.h" | 15 #include "remoting/host/host_exit_codes.h" |
| 16 #include "remoting/host/logging.h" | 16 #include "remoting/host/logging.h" |
| 17 #include "remoting/host/security_key/remote_security_key_ipc_client.h" | 17 #include "remoting/host/security_key/remote_security_key_ipc_client.h" |
| 18 #include "remoting/host/security_key/remote_security_key_message_handler.h" | 18 #include "remoting/host/security_key/remote_security_key_message_handler.h" |
| 19 | 19 |
| 20 #if defined(OS_WIN) |
| 21 #include <aclapi.h> |
| 22 #include <windows.h> |
| 23 |
| 24 #include "base/win/scoped_handle.h" |
| 25 #endif // defined(OS_WIN) |
| 26 |
| 27 #if defined(OS_WIN) |
| 28 namespace { |
| 29 |
| 30 bool AddAccessRightForWellKnownSid(WELL_KNOWN_SID_TYPE type, DWORD new_right) { |
| 31 // Open a handle for the current process, read the current DACL, update it, |
| 32 // and write it back. This will add |new_right| to the current process. |
| 33 base::win::ScopedHandle process_handle(OpenProcess(READ_CONTROL | WRITE_DAC, |
| 34 /*bInheritHandle=*/FALSE, |
| 35 GetCurrentProcessId())); |
| 36 if (!process_handle.IsValid()) { |
| 37 PLOG(ERROR) << "OpenProcess() failed!"; |
| 38 return false; |
| 39 } |
| 40 |
| 41 // TODO(joedow): Add a custom deleter to handle objects which are freed via |
| 42 // LocalFree(). Tracked by crbug.com/622913 |
| 43 PSECURITY_DESCRIPTOR descriptor = nullptr; |
| 44 // |old_dacl| is a pointer into the opaque |descriptor| struct, don't free it. |
| 45 PACL old_dacl = nullptr; |
| 46 PACL new_dacl = nullptr; |
| 47 |
| 48 if (GetSecurityInfo(process_handle.Get(), |
| 49 SE_KERNEL_OBJECT, |
| 50 DACL_SECURITY_INFORMATION, |
| 51 /*ppsidOwner=*/nullptr, |
| 52 /*ppsidGroup=*/nullptr, |
| 53 &old_dacl, |
| 54 /*ppSacl=*/nullptr, |
| 55 &descriptor) != ERROR_SUCCESS) { |
| 56 PLOG(ERROR) << "GetSecurityInfo() failed!"; |
| 57 return false; |
| 58 } |
| 59 |
| 60 BYTE buffer[SECURITY_MAX_SID_SIZE] = {0}; |
| 61 DWORD buffer_size = SECURITY_MAX_SID_SIZE; |
| 62 if (!CreateWellKnownSid(type, /*DomainSid=*/nullptr, buffer, &buffer_size)) { |
| 63 PLOG(ERROR) << "CreateWellKnownSid() failed!"; |
| 64 LocalFree(descriptor); |
| 65 return false; |
| 66 } |
| 67 |
| 68 SID* sid = reinterpret_cast<SID*>(buffer); |
| 69 EXPLICIT_ACCESS new_access = {0}; |
| 70 new_access.grfAccessMode = GRANT_ACCESS; |
| 71 new_access.grfAccessPermissions = new_right; |
| 72 new_access.grfInheritance = NO_INHERITANCE; |
| 73 |
| 74 new_access.Trustee.pMultipleTrustee = nullptr; |
| 75 new_access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; |
| 76 new_access.Trustee.TrusteeForm = TRUSTEE_IS_SID; |
| 77 new_access.Trustee.ptstrName = reinterpret_cast<LPWSTR>(sid); |
| 78 |
| 79 if (SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl) != ERROR_SUCCESS) { |
| 80 PLOG(ERROR) << "SetEntriesInAcl() failed!"; |
| 81 LocalFree(descriptor); |
| 82 return false; |
| 83 } |
| 84 |
| 85 bool right_added = true; |
| 86 if (SetSecurityInfo(process_handle.Get(), |
| 87 SE_KERNEL_OBJECT, |
| 88 DACL_SECURITY_INFORMATION, |
| 89 /*ppsidOwner=*/nullptr, |
| 90 /*ppsidGroup=*/nullptr, |
| 91 new_dacl, |
| 92 /*ppSacl=*/nullptr) != ERROR_SUCCESS) { |
| 93 PLOG(ERROR) << "SetSecurityInfo() failed!"; |
| 94 right_added = false; |
| 95 } |
| 96 |
| 97 LocalFree(new_dacl); |
| 98 LocalFree(descriptor); |
| 99 |
| 100 return right_added; |
| 101 } |
| 102 |
| 103 } // namespace |
| 104 #endif // defined(OS_WIN) |
| 105 |
| 20 namespace remoting { | 106 namespace remoting { |
| 21 | 107 |
| 22 int StartRemoteSecurityKey() { | 108 int StartRemoteSecurityKey() { |
| 23 #if defined(OS_WIN) | 109 #if defined(OS_WIN) |
| 110 if (!AddAccessRightForWellKnownSid(WinLocalServiceSid, |
| 111 PROCESS_QUERY_LIMITED_INFORMATION)) { |
| 112 return kInitializationFailed; |
| 113 } |
| 114 |
| 24 // GetStdHandle() returns pseudo-handles for stdin and stdout even if | 115 // GetStdHandle() returns pseudo-handles for stdin and stdout even if |
| 25 // the hosting executable specifies "Windows" subsystem. However the returned | 116 // the hosting executable specifies "Windows" subsystem. However the returned |
| 26 // handles are invalid in that case unless standard input and output are | 117 // handles are invalid in that case unless standard input and output are |
| 27 // redirected to a pipe or file. | 118 // redirected to a pipe or file. |
| 28 base::File read_file(GetStdHandle(STD_INPUT_HANDLE)); | 119 base::File read_file(GetStdHandle(STD_INPUT_HANDLE)); |
| 29 base::File write_file(GetStdHandle(STD_OUTPUT_HANDLE)); | 120 base::File write_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
| 30 | 121 |
| 31 // After the message handler starts, the remote security key message reader | 122 // After the message handler starts, the remote security key message reader |
| 32 // will keep doing blocking read operations on the input named pipe. | 123 // will keep doing blocking read operations on the input named pipe. |
| 33 // If any other thread tries to perform any operation on STDIN, it will also | 124 // If any other thread tries to perform any operation on STDIN, it will also |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 base::AtExitManager exit_manager; | 156 base::AtExitManager exit_manager; |
| 66 base::MessageLoopForIO message_loop; | 157 base::MessageLoopForIO message_loop; |
| 67 | 158 |
| 68 base::CommandLine::Init(argc, argv); | 159 base::CommandLine::Init(argc, argv); |
| 69 remoting::InitHostLogging(); | 160 remoting::InitHostLogging(); |
| 70 | 161 |
| 71 return StartRemoteSecurityKey(); | 162 return StartRemoteSecurityKey(); |
| 72 } | 163 } |
| 73 | 164 |
| 74 } // namespace remoting | 165 } // namespace remoting |
| OLD | NEW |