| 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 <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" |
| 13 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
| 14 #include "sandbox/src/job.h" | 15 #include "sandbox/src/job.h" |
| 15 #include "sandbox/src/restricted_token.h" | 16 #include "sandbox/src/restricted_token.h" |
| 16 #include "sandbox/src/security_level.h" | 17 #include "sandbox/src/security_level.h" |
| 17 #include "sandbox/src/sid.h" | 18 #include "sandbox/src/sid.h" |
| 18 | 19 |
| 19 namespace sandbox { | 20 namespace sandbox { |
| 20 | 21 |
| 21 DWORD CreateRestrictedToken(HANDLE *token_handle, | 22 DWORD CreateRestrictedToken(HANDLE *token_handle, |
| 22 TokenLevel security_level, | 23 TokenLevel security_level, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 impersonation_level, | 176 impersonation_level, |
| 176 INTEGRITY_LEVEL_LAST, | 177 INTEGRITY_LEVEL_LAST, |
| 177 IMPERSONATION); | 178 IMPERSONATION); |
| 178 if (ERROR_SUCCESS != err_code) { | 179 if (ERROR_SUCCESS != err_code) { |
| 179 return err_code; | 180 return err_code; |
| 180 } | 181 } |
| 181 base::win::ScopedHandle impersonation_token(impersonation_token_handle); | 182 base::win::ScopedHandle impersonation_token(impersonation_token_handle); |
| 182 | 183 |
| 183 // Start the process | 184 // Start the process |
| 184 STARTUPINFO startup_info = {0}; | 185 STARTUPINFO startup_info = {0}; |
| 185 PROCESS_INFORMATION process_info = {0}; | 186 base::win::ScopedProcessInformation process_info; |
| 186 DWORD flags = CREATE_SUSPENDED; | 187 DWORD flags = CREATE_SUSPENDED; |
| 187 | 188 |
| 188 if (base::win::GetVersion() < base::win::VERSION_WIN8) { | 189 if (base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 189 // Windows 8 implements nested jobs, but for older systems we need to | 190 // Windows 8 implements nested jobs, but for older systems we need to |
| 190 // break out of any job we're in to enforce our restrictions. | 191 // break out of any job we're in to enforce our restrictions. |
| 191 flags |= CREATE_BREAKAWAY_FROM_JOB; | 192 flags |= CREATE_BREAKAWAY_FROM_JOB; |
| 192 } | 193 } |
| 193 | 194 |
| 194 if (!::CreateProcessAsUser(primary_token.Get(), | 195 if (!::CreateProcessAsUser(primary_token.Get(), |
| 195 NULL, // No application name. | 196 NULL, // No application name. |
| 196 command_line, | 197 command_line, |
| 197 NULL, // No security attribute. | 198 NULL, // No security attribute. |
| 198 NULL, // No thread attribute. | 199 NULL, // No thread attribute. |
| 199 FALSE, // Do not inherit handles. | 200 FALSE, // Do not inherit handles. |
| 200 flags, | 201 flags, |
| 201 NULL, // Use the environment of the caller. | 202 NULL, // Use the environment of the caller. |
| 202 NULL, // Use current directory of the caller. | 203 NULL, // Use current directory of the caller. |
| 203 &startup_info, | 204 &startup_info, |
| 204 &process_info)) { | 205 process_info.Receive())) { |
| 205 return ::GetLastError(); | 206 return ::GetLastError(); |
| 206 } | 207 } |
| 207 | 208 |
| 208 base::win::ScopedHandle thread_handle(process_info.hThread); | |
| 209 base::win::ScopedHandle process_handle(process_info.hProcess); | |
| 210 | |
| 211 // Change the token of the main thread of the new process for the | 209 // Change the token of the main thread of the new process for the |
| 212 // impersonation token with more rights. | 210 // impersonation token with more rights. |
| 213 if (!::SetThreadToken(&process_info.hThread, impersonation_token.Get())) { | 211 { |
| 214 ::TerminateProcess(process_handle.Get(), | 212 HANDLE temp_thread = process_info.thread_handle(); |
| 215 0); // exit code | 213 if (!::SetThreadToken(&temp_thread, impersonation_token.Get())) { |
| 216 return ::GetLastError(); | 214 ::TerminateProcess(process_info.process_handle(), |
| 215 0); // exit code |
| 216 return ::GetLastError(); |
| 217 } |
| 217 } | 218 } |
| 218 | 219 |
| 219 err_code = job.AssignProcessToJob(process_handle.Get()); | 220 err_code = job.AssignProcessToJob(process_info.process_handle()); |
| 220 if (ERROR_SUCCESS != err_code) { | 221 if (ERROR_SUCCESS != err_code) { |
| 221 ::TerminateProcess(process_handle.Get(), | 222 ::TerminateProcess(process_info.process_handle(), |
| 222 0); // exit code | 223 0); // exit code |
| 223 return ::GetLastError(); | 224 return ::GetLastError(); |
| 224 } | 225 } |
| 225 | 226 |
| 226 // Start the application | 227 // Start the application |
| 227 ::ResumeThread(thread_handle.Get()); | 228 ::ResumeThread(process_info.thread_handle()); |
| 228 | 229 |
| 229 (*job_handle_ret) = job.Detach(); | 230 (*job_handle_ret) = job.Detach(); |
| 230 | 231 |
| 231 return ERROR_SUCCESS; | 232 return ERROR_SUCCESS; |
| 232 } | 233 } |
| 233 | 234 |
| 234 DWORD SetObjectIntegrityLabel(HANDLE handle, SE_OBJECT_TYPE type, | 235 DWORD SetObjectIntegrityLabel(HANDLE handle, SE_OBJECT_TYPE type, |
| 235 const wchar_t* ace_access, | 236 const wchar_t* ace_access, |
| 236 const wchar_t* integrity_level_sid) { | 237 const wchar_t* integrity_level_sid) { |
| 237 // Build the SDDL string for the label. | 238 // Build the SDDL string for the label. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 if (!::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_DEFAULT, | 335 if (!::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_DEFAULT, |
| 335 &token_handle)) | 336 &token_handle)) |
| 336 return ::GetLastError(); | 337 return ::GetLastError(); |
| 337 | 338 |
| 338 base::win::ScopedHandle token(token_handle); | 339 base::win::ScopedHandle token(token_handle); |
| 339 | 340 |
| 340 return SetTokenIntegrityLevel(token.Get(), integrity_level); | 341 return SetTokenIntegrityLevel(token.Get(), integrity_level); |
| 341 } | 342 } |
| 342 | 343 |
| 343 } // namespace sandbox | 344 } // namespace sandbox |
| OLD | NEW |