Chromium Code Reviews| 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 { | |
|
Hzj_jie
2016/06/23 22:35:58
I believe we usually add a blank line after "names
joedow
2016/06/23 23:07:18
Done.
| |
| 29 bool AddAccessRightForWellKnownSid(WELL_KNOWN_SID_TYPE type, DWORD new_right) { | |
|
Hzj_jie
2016/06/23 22:35:58
I saw this function has only been used with WinLoc
joedow
2016/06/23 23:07:17
There are two reasons:
- When someone reads the ca
| |
| 30 // Open a handle for the current process, read the current DACL, update it, | |
| 31 // and write it back. This will add |new_right| to the current process. | |
| 32 base::win::ScopedHandle process_handle(OpenProcess(READ_CONTROL | WRITE_DAC, | |
| 33 /*bInheritHandle=*/FALSE, | |
| 34 GetCurrentProcessId())); | |
| 35 if (!process_handle.IsValid()) { | |
| 36 PLOG(ERROR) << "OpenProcess() failed!"; | |
| 37 return false; | |
| 38 } | |
| 39 | |
| 40 PSECURITY_DESCRIPTOR descriptor = nullptr; | |
| 41 // |old_dacl| is a pointer into the opaque |descriptor| struct, don't free it. | |
| 42 PACL old_dacl = nullptr; | |
| 43 PACL new_dacl = nullptr; | |
|
Sergey Ulanov
2016/06/23 22:28:41
move this below, next to the SetEntriesInAcl() cal
joedow
2016/06/23 23:07:18
Done.
| |
| 44 if (GetSecurityInfo(process_handle.Get(), | |
| 45 SE_KERNEL_OBJECT, | |
| 46 DACL_SECURITY_INFORMATION, | |
| 47 /*ppsidOwner=*/nullptr, | |
| 48 /*ppsidGroup=*/nullptr, | |
| 49 &old_dacl, | |
| 50 /*ppSacl=*/nullptr, | |
| 51 &descriptor) != ERROR_SUCCESS) { | |
| 52 PLOG(ERROR) << "GetSecurityInfo() failed!"; | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 BYTE buffer[SECURITY_MAX_SID_SIZE] = {0}; | |
| 57 DWORD buffer_size = SECURITY_MAX_SID_SIZE; | |
| 58 if (!CreateWellKnownSid(type, /*DomainSid=*/nullptr, buffer, &buffer_size)) { | |
| 59 PLOG(ERROR) << "CreateWellKnownSid() failed!"; | |
| 60 LocalFree(descriptor); | |
|
Sergey Ulanov
2016/06/23 22:28:41
do you need to free old_nacl here and below?
Hzj_jie
2016/06/23 22:35:58
You can use unique_ptr with a custom deleter to av
Sergey Ulanov
2016/06/23 22:43:40
+1, that would be the best
joedow
2016/06/23 23:07:17
Acknowledged. I looked into this and it was more
joedow
2016/06/23 23:07:18
old_dacl is a pointer into an opaque struct (|desc
| |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 SID* sid = reinterpret_cast<SID*>(buffer); | |
| 65 EXPLICIT_ACCESS new_access = {0}; | |
| 66 new_access.grfAccessMode = GRANT_ACCESS; | |
| 67 new_access.grfAccessPermissions = new_right; | |
| 68 new_access.grfInheritance = NO_INHERITANCE; | |
| 69 | |
| 70 new_access.Trustee.pMultipleTrustee = nullptr; | |
| 71 new_access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; | |
| 72 new_access.Trustee.TrusteeForm = TRUSTEE_IS_SID; | |
| 73 new_access.Trustee.ptstrName = reinterpret_cast<LPWSTR>(sid); | |
| 74 | |
| 75 if (ERROR_SUCCESS != SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl)) { | |
|
Sergey Ulanov
2016/06/23 22:28:41
nit: move ERROR_SUCCESS to the end, i.e. (SetEntri
joedow
2016/06/23 23:07:17
Done.
| |
| 76 PLOG(ERROR) << "SetEntriesInAcl() failed!"; | |
| 77 LocalFree(descriptor); | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 bool right_added = true; | |
| 82 if (SetSecurityInfo(process_handle.Get(), | |
| 83 SE_KERNEL_OBJECT, | |
| 84 DACL_SECURITY_INFORMATION, | |
| 85 /*ppsidOwner=*/nullptr, | |
| 86 /*ppsidGroup=*/nullptr, | |
| 87 new_dacl, | |
| 88 /*ppSacl=*/nullptr) != ERROR_SUCCESS) { | |
| 89 PLOG(ERROR) << "SetSecurityInfo() failed!"; | |
| 90 right_added = false; | |
| 91 } | |
| 92 | |
| 93 LocalFree(new_dacl); | |
| 94 LocalFree(descriptor); | |
| 95 | |
| 96 return right_added; | |
| 97 } | |
| 98 } // namespace | |
| 99 #endif // defined(OS_WIN) | |
| 100 | |
| 20 namespace remoting { | 101 namespace remoting { |
| 21 | 102 |
| 22 int StartRemoteSecurityKey() { | 103 int StartRemoteSecurityKey() { |
| 23 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
| 105 if (!AddAccessRightForWellKnownSid(WinLocalServiceSid, | |
| 106 PROCESS_QUERY_LIMITED_INFORMATION)) { | |
| 107 return false; | |
|
Hzj_jie
2016/06/23 22:35:58
The return type is int, not bool.
joedow
2016/06/23 23:07:17
Ha, good catch :)
| |
| 108 } | |
| 109 | |
| 24 // GetStdHandle() returns pseudo-handles for stdin and stdout even if | 110 // GetStdHandle() returns pseudo-handles for stdin and stdout even if |
| 25 // the hosting executable specifies "Windows" subsystem. However the returned | 111 // the hosting executable specifies "Windows" subsystem. However the returned |
| 26 // handles are invalid in that case unless standard input and output are | 112 // handles are invalid in that case unless standard input and output are |
| 27 // redirected to a pipe or file. | 113 // redirected to a pipe or file. |
| 28 base::File read_file(GetStdHandle(STD_INPUT_HANDLE)); | 114 base::File read_file(GetStdHandle(STD_INPUT_HANDLE)); |
| 29 base::File write_file(GetStdHandle(STD_OUTPUT_HANDLE)); | 115 base::File write_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
| 30 | 116 |
| 31 // After the message handler starts, the remote security key message reader | 117 // After the message handler starts, the remote security key message reader |
| 32 // will keep doing blocking read operations on the input named pipe. | 118 // 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 | 119 // 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; | 151 base::AtExitManager exit_manager; |
| 66 base::MessageLoopForIO message_loop; | 152 base::MessageLoopForIO message_loop; |
| 67 | 153 |
| 68 base::CommandLine::Init(argc, argv); | 154 base::CommandLine::Init(argc, argv); |
| 69 remoting::InitHostLogging(); | 155 remoting::InitHostLogging(); |
| 70 | 156 |
| 71 return StartRemoteSecurityKey(); | 157 return StartRemoteSecurityKey(); |
| 72 } | 158 } |
| 73 | 159 |
| 74 } // namespace remoting | 160 } // namespace remoting |
| OLD | NEW |