| OLD | NEW |
| 1 // Copyright (c) 2010 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/win/scoped_process_information.h" |
| 5 #include "base/win/windows_version.h" | 6 #include "base/win/windows_version.h" |
| 6 #include "sandbox/src/sandbox.h" | 7 #include "sandbox/src/sandbox.h" |
| 7 #include "sandbox/src/sandbox_factory.h" | 8 #include "sandbox/src/sandbox_factory.h" |
| 8 #include "sandbox/src/sandbox_utils.h" | 9 #include "sandbox/src/sandbox_utils.h" |
| 9 #include "sandbox/src/target_services.h" | 10 #include "sandbox/src/target_services.h" |
| 10 #include "sandbox/tests/common/controller.h" | 11 #include "sandbox/tests/common/controller.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace sandbox { | 14 namespace sandbox { |
| 14 | 15 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 return ::GetLastError(); | 143 return ::GetLastError(); |
| 143 | 144 |
| 144 return SBOX_TEST_SUCCEEDED; | 145 return SBOX_TEST_SUCCEEDED; |
| 145 } | 146 } |
| 146 | 147 |
| 147 // Tests that we can call CreateProcess. | 148 // Tests that we can call CreateProcess. |
| 148 SBOX_TESTS_COMMAND int PolicyTargetTest_process(int argc, wchar_t **argv) { | 149 SBOX_TESTS_COMMAND int PolicyTargetTest_process(int argc, wchar_t **argv) { |
| 149 // Use default values to create a new process. | 150 // Use default values to create a new process. |
| 150 STARTUPINFO startup_info = {0}; | 151 STARTUPINFO startup_info = {0}; |
| 151 startup_info.cb = sizeof(startup_info); | 152 startup_info.cb = sizeof(startup_info); |
| 152 PROCESS_INFORMATION process_info; | 153 base::win::ScopedProcessInformation process_info; |
| 153 if (!::CreateProcessW(L"foo.exe", L"foo.exe", NULL, NULL, FALSE, 0, | 154 if (!::CreateProcessW(L"foo.exe", L"foo.exe", NULL, NULL, FALSE, 0, |
| 154 NULL, NULL, &startup_info, &process_info)) | 155 NULL, NULL, &startup_info, process_info.Receive())) |
| 155 return SBOX_TEST_SUCCEEDED; | 156 return SBOX_TEST_SUCCEEDED; |
| 156 return SBOX_TEST_FAILED; | 157 return SBOX_TEST_FAILED; |
| 157 } | 158 } |
| 158 | 159 |
| 159 TEST(PolicyTargetTest, SetInformationThread) { | 160 TEST(PolicyTargetTest, SetInformationThread) { |
| 160 TestRunner runner; | 161 TestRunner runner; |
| 161 if (base::win::GetVersion() >= base::win::VERSION_XP) { | 162 if (base::win::GetVersion() >= base::win::VERSION_XP) { |
| 162 runner.SetTestState(BEFORE_REVERT); | 163 runner.SetTestState(BEFORE_REVERT); |
| 163 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"PolicyTargetTest_token")); | 164 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"PolicyTargetTest_token")); |
| 164 } | 165 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 // Get the path to the sandboxed app. | 227 // Get the path to the sandboxed app. |
| 227 wchar_t prog_name[MAX_PATH]; | 228 wchar_t prog_name[MAX_PATH]; |
| 228 GetModuleFileNameW(NULL, prog_name, MAX_PATH); | 229 GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
| 229 | 230 |
| 230 std::wstring arguments(L"\""); | 231 std::wstring arguments(L"\""); |
| 231 arguments += prog_name; | 232 arguments += prog_name; |
| 232 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. | 233 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. |
| 233 | 234 |
| 234 // Launch the app. | 235 // Launch the app. |
| 235 ResultCode result = SBOX_ALL_OK; | 236 ResultCode result = SBOX_ALL_OK; |
| 236 PROCESS_INFORMATION target = {0}; | 237 base::win::ScopedProcessInformation target; |
| 237 | 238 |
| 238 TargetPolicy* policy = broker->CreatePolicy(); | 239 TargetPolicy* policy = broker->CreatePolicy(); |
| 239 policy->SetAlternateDesktop(false); | 240 policy->SetAlternateDesktop(false); |
| 240 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); | 241 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); |
| 241 result = broker->SpawnTarget(prog_name, arguments.c_str(), policy, &target); | 242 result = broker->SpawnTarget( |
| 243 prog_name, arguments.c_str(), policy, target.Receive()); |
| 242 policy->Release(); | 244 policy->Release(); |
| 243 | 245 |
| 244 EXPECT_EQ(SBOX_ALL_OK, result); | 246 EXPECT_EQ(SBOX_ALL_OK, result); |
| 245 | 247 |
| 246 EXPECT_EQ(1, ::ResumeThread(target.hThread)); | 248 EXPECT_EQ(1, ::ResumeThread(target.thread_handle())); |
| 247 | 249 |
| 248 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.hProcess, 2000)); | 250 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.process_handle(), 2000)); |
| 249 | 251 |
| 250 EXPECT_NE(::GetThreadDesktop(target.dwThreadId), | 252 EXPECT_NE(::GetThreadDesktop(target.thread_id()), |
| 251 ::GetThreadDesktop(::GetCurrentThreadId())); | 253 ::GetThreadDesktop(::GetCurrentThreadId())); |
| 252 | 254 |
| 253 std::wstring desktop_name = policy->GetAlternateDesktop(); | 255 std::wstring desktop_name = policy->GetAlternateDesktop(); |
| 254 HDESK desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); | 256 HDESK desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); |
| 255 EXPECT_TRUE(NULL != desk); | 257 EXPECT_TRUE(NULL != desk); |
| 256 EXPECT_TRUE(::CloseDesktop(desk)); | 258 EXPECT_TRUE(::CloseDesktop(desk)); |
| 257 EXPECT_TRUE(::TerminateProcess(target.hProcess, 0)); | 259 EXPECT_TRUE(::TerminateProcess(target.process_handle(), 0)); |
| 258 | 260 |
| 259 ::WaitForSingleObject(target.hProcess, INFINITE); | 261 ::WaitForSingleObject(target.process_handle(), INFINITE); |
| 260 | |
| 261 EXPECT_TRUE(::CloseHandle(target.hProcess)); | |
| 262 EXPECT_TRUE(::CloseHandle(target.hThread)); | |
| 263 | 262 |
| 264 // Close the desktop handle. | 263 // Close the desktop handle. |
| 265 temp_policy = broker->CreatePolicy(); | 264 temp_policy = broker->CreatePolicy(); |
| 266 temp_policy->DestroyAlternateDesktop(); | 265 temp_policy->DestroyAlternateDesktop(); |
| 267 temp_policy->Release(); | 266 temp_policy->Release(); |
| 268 | 267 |
| 269 // Make sure the desktop does not exist anymore. | 268 // Make sure the desktop does not exist anymore. |
| 270 desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); | 269 desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); |
| 271 EXPECT_TRUE(NULL == desk); | 270 EXPECT_TRUE(NULL == desk); |
| 272 } | 271 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 288 // Get the path to the sandboxed app. | 287 // Get the path to the sandboxed app. |
| 289 wchar_t prog_name[MAX_PATH]; | 288 wchar_t prog_name[MAX_PATH]; |
| 290 GetModuleFileNameW(NULL, prog_name, MAX_PATH); | 289 GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
| 291 | 290 |
| 292 std::wstring arguments(L"\""); | 291 std::wstring arguments(L"\""); |
| 293 arguments += prog_name; | 292 arguments += prog_name; |
| 294 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. | 293 arguments += L"\" -child 0 wait"; // Don't care about the "state" argument. |
| 295 | 294 |
| 296 // Launch the app. | 295 // Launch the app. |
| 297 ResultCode result = SBOX_ALL_OK; | 296 ResultCode result = SBOX_ALL_OK; |
| 298 PROCESS_INFORMATION target = {0}; | 297 base::win::ScopedProcessInformation target; |
| 299 | 298 |
| 300 TargetPolicy* policy = broker->CreatePolicy(); | 299 TargetPolicy* policy = broker->CreatePolicy(); |
| 301 policy->SetAlternateDesktop(true); | 300 policy->SetAlternateDesktop(true); |
| 302 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); | 301 policy->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); |
| 303 result = broker->SpawnTarget(prog_name, arguments.c_str(), policy, &target); | 302 result = broker->SpawnTarget( |
| 303 prog_name, arguments.c_str(), policy, target.Receive()); |
| 304 policy->Release(); | 304 policy->Release(); |
| 305 | 305 |
| 306 EXPECT_EQ(SBOX_ALL_OK, result); | 306 EXPECT_EQ(SBOX_ALL_OK, result); |
| 307 | 307 |
| 308 EXPECT_EQ(1, ::ResumeThread(target.hThread)); | 308 EXPECT_EQ(1, ::ResumeThread(target.thread_handle())); |
| 309 | 309 |
| 310 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.hProcess, 2000)); | 310 EXPECT_EQ(WAIT_TIMEOUT, ::WaitForSingleObject(target.process_handle(), 2000)); |
| 311 | 311 |
| 312 EXPECT_NE(::GetThreadDesktop(target.dwThreadId), | 312 EXPECT_NE(::GetThreadDesktop(target.thread_id()), |
| 313 ::GetThreadDesktop(::GetCurrentThreadId())); | 313 ::GetThreadDesktop(::GetCurrentThreadId())); |
| 314 | 314 |
| 315 std::wstring desktop_name = policy->GetAlternateDesktop(); | 315 std::wstring desktop_name = policy->GetAlternateDesktop(); |
| 316 ASSERT_FALSE(desktop_name.empty()); | 316 ASSERT_FALSE(desktop_name.empty()); |
| 317 | 317 |
| 318 // Make sure there is a backslash, for the window station name. | 318 // Make sure there is a backslash, for the window station name. |
| 319 EXPECT_NE(desktop_name.find_first_of(L'\\'), std::wstring::npos); | 319 EXPECT_NE(desktop_name.find_first_of(L'\\'), std::wstring::npos); |
| 320 | 320 |
| 321 // Isolate the desktop name. | 321 // Isolate the desktop name. |
| 322 desktop_name = desktop_name.substr(desktop_name.find_first_of(L'\\') + 1); | 322 desktop_name = desktop_name.substr(desktop_name.find_first_of(L'\\') + 1); |
| 323 | 323 |
| 324 HDESK desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); | 324 HDESK desk = ::OpenDesktop(desktop_name.c_str(), 0, FALSE, DESKTOP_ENUMERATE); |
| 325 // This should fail if the desktop is really on another window station. | 325 // This should fail if the desktop is really on another window station. |
| 326 EXPECT_FALSE(NULL != desk); | 326 EXPECT_FALSE(NULL != desk); |
| 327 EXPECT_TRUE(::TerminateProcess(target.hProcess, 0)); | 327 EXPECT_TRUE(::TerminateProcess(target.process_handle(), 0)); |
| 328 | 328 |
| 329 ::WaitForSingleObject(target.hProcess, INFINITE); | 329 ::WaitForSingleObject(target.process_handle(), INFINITE); |
| 330 | |
| 331 EXPECT_TRUE(::CloseHandle(target.hProcess)); | |
| 332 EXPECT_TRUE(::CloseHandle(target.hThread)); | |
| 333 | 330 |
| 334 // Close the desktop handle. | 331 // Close the desktop handle. |
| 335 temp_policy = broker->CreatePolicy(); | 332 temp_policy = broker->CreatePolicy(); |
| 336 temp_policy->DestroyAlternateDesktop(); | 333 temp_policy->DestroyAlternateDesktop(); |
| 337 temp_policy->Release(); | 334 temp_policy->Release(); |
| 338 } | 335 } |
| 339 | 336 |
| 340 } // namespace sandbox | 337 } // namespace sandbox |
| OLD | NEW |