| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <aclapi.h> | 5 #include <aclapi.h> |
| 6 #include <sddl.h> | 6 #include <sddl.h> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "sandbox/src/restricted_token_utils.h" | 9 #include "sandbox/src/restricted_token_utils.h" |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/win/scoped_handle.h" | 12 #include "base/win/scoped_handle.h" |
| 13 #include "base/win/scoped_process_information.h" | |
| 14 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 15 #include "sandbox/src/job.h" | 14 #include "sandbox/src/job.h" |
| 16 #include "sandbox/src/restricted_token.h" | 15 #include "sandbox/src/restricted_token.h" |
| 17 #include "sandbox/src/security_level.h" | 16 #include "sandbox/src/security_level.h" |
| 18 #include "sandbox/src/sid.h" | 17 #include "sandbox/src/sid.h" |
| 19 | 18 |
| 20 namespace sandbox { | 19 namespace sandbox { |
| 21 | 20 |
| 22 DWORD CreateRestrictedToken(HANDLE *token_handle, | 21 DWORD CreateRestrictedToken(HANDLE *token_handle, |
| 23 TokenLevel security_level, | 22 TokenLevel security_level, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 impersonation_level, | 175 impersonation_level, |
| 177 INTEGRITY_LEVEL_LAST, | 176 INTEGRITY_LEVEL_LAST, |
| 178 IMPERSONATION); | 177 IMPERSONATION); |
| 179 if (ERROR_SUCCESS != err_code) { | 178 if (ERROR_SUCCESS != err_code) { |
| 180 return err_code; | 179 return err_code; |
| 181 } | 180 } |
| 182 base::win::ScopedHandle impersonation_token(impersonation_token_handle); | 181 base::win::ScopedHandle impersonation_token(impersonation_token_handle); |
| 183 | 182 |
| 184 // Start the process | 183 // Start the process |
| 185 STARTUPINFO startup_info = {0}; | 184 STARTUPINFO startup_info = {0}; |
| 186 base::win::ScopedProcessInformation process_info; | 185 PROCESS_INFORMATION process_info = {0}; |
| 187 | 186 |
| 188 if (!::CreateProcessAsUser(primary_token.Get(), | 187 if (!::CreateProcessAsUser(primary_token.Get(), |
| 189 NULL, // No application name. | 188 NULL, // No application name. |
| 190 command_line, | 189 command_line, |
| 191 NULL, // No security attribute. | 190 NULL, // No security attribute. |
| 192 NULL, // No thread attribute. | 191 NULL, // No thread attribute. |
| 193 FALSE, // Do not inherit handles. | 192 FALSE, // Do not inherit handles. |
| 194 CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB, | 193 CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB, |
| 195 NULL, // Use the environment of the caller. | 194 NULL, // Use the environment of the caller. |
| 196 NULL, // Use current directory of the caller. | 195 NULL, // Use current directory of the caller. |
| 197 &startup_info, | 196 &startup_info, |
| 198 process_info.Receive())) { | 197 &process_info)) { |
| 199 return ::GetLastError(); | 198 return ::GetLastError(); |
| 200 } | 199 } |
| 201 | 200 |
| 201 base::win::ScopedHandle thread_handle(process_info.hThread); |
| 202 base::win::ScopedHandle process_handle(process_info.hProcess); |
| 203 |
| 202 // Change the token of the main thread of the new process for the | 204 // Change the token of the main thread of the new process for the |
| 203 // impersonation token with more rights. | 205 // impersonation token with more rights. |
| 204 { | 206 if (!::SetThreadToken(&process_info.hThread, impersonation_token.Get())) { |
| 205 HANDLE temp_thread = process_info.thread_handle(); | 207 ::TerminateProcess(process_handle.Get(), |
| 206 if (!::SetThreadToken(&temp_thread, impersonation_token.Get())) { | 208 0); // exit code |
| 207 ::TerminateProcess(process_info.process_handle(), | 209 return ::GetLastError(); |
| 208 0); // exit code | |
| 209 return ::GetLastError(); | |
| 210 } | |
| 211 } | 210 } |
| 212 | 211 |
| 213 err_code = job.AssignProcessToJob(process_info.process_handle()); | 212 err_code = job.AssignProcessToJob(process_handle.Get()); |
| 214 if (ERROR_SUCCESS != err_code) { | 213 if (ERROR_SUCCESS != err_code) { |
| 215 ::TerminateProcess(process_info.process_handle(), | 214 ::TerminateProcess(process_handle.Get(), |
| 216 0); // exit code | 215 0); // exit code |
| 217 return ::GetLastError(); | 216 return ::GetLastError(); |
| 218 } | 217 } |
| 219 | 218 |
| 220 // Start the application | 219 // Start the application |
| 221 ::ResumeThread(process_info.thread_handle()); | 220 ::ResumeThread(thread_handle.Get()); |
| 222 | 221 |
| 223 (*job_handle_ret) = job.Detach(); | 222 (*job_handle_ret) = job.Detach(); |
| 224 | 223 |
| 225 return ERROR_SUCCESS; | 224 return ERROR_SUCCESS; |
| 226 } | 225 } |
| 227 | 226 |
| 228 DWORD SetObjectIntegrityLabel(HANDLE handle, SE_OBJECT_TYPE type, | 227 DWORD SetObjectIntegrityLabel(HANDLE handle, SE_OBJECT_TYPE type, |
| 229 const wchar_t* ace_access, | 228 const wchar_t* ace_access, |
| 230 const wchar_t* integrity_level_sid) { | 229 const wchar_t* integrity_level_sid) { |
| 231 // Build the SDDL string for the label. | 230 // Build the SDDL string for the label. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 if (!::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_DEFAULT, | 327 if (!::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_DEFAULT, |
| 329 &token_handle)) | 328 &token_handle)) |
| 330 return ::GetLastError(); | 329 return ::GetLastError(); |
| 331 | 330 |
| 332 base::win::ScopedHandle token(token_handle); | 331 base::win::ScopedHandle token(token_handle); |
| 333 | 332 |
| 334 return SetTokenIntegrityLevel(token.Get(), integrity_level); | 333 return SetTokenIntegrityLevel(token.Get(), integrity_level); |
| 335 } | 334 } |
| 336 | 335 |
| 337 } // namespace sandbox | 336 } // namespace sandbox |
| OLD | NEW |