| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/process_util.h" | 5 #include "base/process_util.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <io.h> | 8 #include <io.h> |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <userenv.h> | 10 #include <userenv.h> |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 93 } |
| 94 if (freopen("CONOUT$", "w", stderr)) { | 94 if (freopen("CONOUT$", "w", stderr)) { |
| 95 setvbuf(stderr, NULL, _IOLBF, kOutputBufferSize); | 95 setvbuf(stderr, NULL, _IOLBF, kOutputBufferSize); |
| 96 _dup2(_fileno(stderr), 2); | 96 _dup2(_fileno(stderr), 2); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog. | 99 // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog. |
| 100 std::ios::sync_with_stdio(); | 100 std::ios::sync_with_stdio(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 ProcessId GetCurrentProcId() { | |
| 104 return ::GetCurrentProcessId(); | |
| 105 } | |
| 106 | |
| 107 ProcessHandle GetCurrentProcessHandle() { | |
| 108 return ::GetCurrentProcess(); | |
| 109 } | |
| 110 | |
| 111 bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle) { | |
| 112 // We try to limit privileges granted to the handle. If you need this | |
| 113 // for test code, consider using OpenPrivilegedProcessHandle instead of | |
| 114 // adding more privileges here. | |
| 115 ProcessHandle result = OpenProcess(PROCESS_TERMINATE | | |
| 116 PROCESS_QUERY_INFORMATION | | |
| 117 SYNCHRONIZE, | |
| 118 FALSE, pid); | |
| 119 | |
| 120 if (result == NULL) | |
| 121 return false; | |
| 122 | |
| 123 *handle = result; | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle) { | |
| 128 ProcessHandle result = OpenProcess(PROCESS_DUP_HANDLE | | |
| 129 PROCESS_TERMINATE | | |
| 130 PROCESS_QUERY_INFORMATION | | |
| 131 PROCESS_VM_READ | | |
| 132 SYNCHRONIZE, | |
| 133 FALSE, pid); | |
| 134 | |
| 135 if (result == NULL) | |
| 136 return false; | |
| 137 | |
| 138 *handle = result; | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 bool OpenProcessHandleWithAccess(ProcessId pid, | |
| 143 uint32 access_flags, | |
| 144 ProcessHandle* handle) { | |
| 145 ProcessHandle result = OpenProcess(access_flags, FALSE, pid); | |
| 146 | |
| 147 if (result == NULL) | |
| 148 return false; | |
| 149 | |
| 150 *handle = result; | |
| 151 return true; | |
| 152 } | |
| 153 | |
| 154 void CloseProcessHandle(ProcessHandle process) { | |
| 155 CloseHandle(process); | |
| 156 } | |
| 157 | |
| 158 ProcessId GetProcId(ProcessHandle process) { | |
| 159 // This returns 0 if we have insufficient rights to query the process handle. | |
| 160 return GetProcessId(process); | |
| 161 } | |
| 162 | |
| 163 bool GetProcessIntegrityLevel(ProcessHandle process, IntegrityLevel *level) { | |
| 164 if (!level) | |
| 165 return false; | |
| 166 | |
| 167 if (win::GetVersion() < base::win::VERSION_VISTA) | |
| 168 return false; | |
| 169 | |
| 170 HANDLE process_token; | |
| 171 if (!OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE, | |
| 172 &process_token)) | |
| 173 return false; | |
| 174 | |
| 175 win::ScopedHandle scoped_process_token(process_token); | |
| 176 | |
| 177 DWORD token_info_length = 0; | |
| 178 if (GetTokenInformation(process_token, TokenIntegrityLevel, NULL, 0, | |
| 179 &token_info_length) || | |
| 180 GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
| 181 return false; | |
| 182 | |
| 183 scoped_ptr<char[]> token_label_bytes(new char[token_info_length]); | |
| 184 if (!token_label_bytes.get()) | |
| 185 return false; | |
| 186 | |
| 187 TOKEN_MANDATORY_LABEL* token_label = | |
| 188 reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get()); | |
| 189 if (!token_label) | |
| 190 return false; | |
| 191 | |
| 192 if (!GetTokenInformation(process_token, TokenIntegrityLevel, token_label, | |
| 193 token_info_length, &token_info_length)) | |
| 194 return false; | |
| 195 | |
| 196 DWORD integrity_level = *GetSidSubAuthority(token_label->Label.Sid, | |
| 197 (DWORD)(UCHAR)(*GetSidSubAuthorityCount(token_label->Label.Sid)-1)); | |
| 198 | |
| 199 if (integrity_level < SECURITY_MANDATORY_MEDIUM_RID) { | |
| 200 *level = LOW_INTEGRITY; | |
| 201 } else if (integrity_level >= SECURITY_MANDATORY_MEDIUM_RID && | |
| 202 integrity_level < SECURITY_MANDATORY_HIGH_RID) { | |
| 203 *level = MEDIUM_INTEGRITY; | |
| 204 } else if (integrity_level >= SECURITY_MANDATORY_HIGH_RID) { | |
| 205 *level = HIGH_INTEGRITY; | |
| 206 } else { | |
| 207 NOTREACHED(); | |
| 208 return false; | |
| 209 } | |
| 210 | |
| 211 return true; | |
| 212 } | |
| 213 | |
| 214 bool LaunchProcess(const string16& cmdline, | 103 bool LaunchProcess(const string16& cmdline, |
| 215 const LaunchOptions& options, | 104 const LaunchOptions& options, |
| 216 ProcessHandle* process_handle) { | 105 ProcessHandle* process_handle) { |
| 217 STARTUPINFO startup_info = {}; | 106 STARTUPINFO startup_info = {}; |
| 218 startup_info.cb = sizeof(startup_info); | 107 startup_info.cb = sizeof(startup_info); |
| 219 if (options.empty_desktop_name) | 108 if (options.empty_desktop_name) |
| 220 startup_info.lpDesktop = L""; | 109 startup_info.lpDesktop = L""; |
| 221 startup_info.dwFlags = STARTF_USESHOWWINDOW; | 110 startup_info.dwFlags = STARTF_USESHOWWINDOW; |
| 222 startup_info.wShowWindow = options.start_hidden ? SW_HIDE : SW_SHOW; | 111 startup_info.wShowWindow = options.start_hidden ? SW_HIDE : SW_SHOW; |
| 223 | 112 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 WaitForSingleObject(proc_info.process_handle(), INFINITE); | 273 WaitForSingleObject(proc_info.process_handle(), INFINITE); |
| 385 | 274 |
| 386 return true; | 275 return true; |
| 387 } | 276 } |
| 388 | 277 |
| 389 void RaiseProcessToHighPriority() { | 278 void RaiseProcessToHighPriority() { |
| 390 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); | 279 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); |
| 391 } | 280 } |
| 392 | 281 |
| 393 } // namespace base | 282 } // namespace base |
| OLD | NEW |