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 <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "base/win/scoped_handle.h" | 11 #include "base/win/scoped_handle.h" |
12 #include "base/win/scoped_process_information.h" | 12 #include "base/win/scoped_process_information.h" |
13 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 14 #include "sandbox/win/src/process_thread_interception.h" |
14 #include "sandbox/win/src/sandbox.h" | 15 #include "sandbox/win/src/sandbox.h" |
15 #include "sandbox/win/src/sandbox_factory.h" | 16 #include "sandbox/win/src/sandbox_factory.h" |
16 #include "sandbox/win/src/sandbox_policy.h" | 17 #include "sandbox/win/src/sandbox_policy.h" |
17 #include "sandbox/win/tests/common/controller.h" | 18 #include "sandbox/win/tests/common/controller.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Creates a process with the |exe| and |command| parameter using the | 23 // Creates a process with the |exe| and |command| parameter using the |
23 // unicode and ascii version of the api. | 24 // unicode and ascii version of the api. |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 return SBOX_TEST_DENIED; | 263 return SBOX_TEST_DENIED; |
263 } | 264 } |
264 } else { | 265 } else { |
265 ::CloseHandle(token); | 266 ::CloseHandle(token); |
266 return SBOX_TEST_SUCCEEDED; | 267 return SBOX_TEST_SUCCEEDED; |
267 } | 268 } |
268 | 269 |
269 return SBOX_TEST_FAILED; | 270 return SBOX_TEST_FAILED; |
270 } | 271 } |
271 | 272 |
| 273 // Generate a event name, used to test thread creation. |
| 274 std::wstring GenerateEventName(DWORD pid) { |
| 275 wchar_t buff[30] = {0}; |
| 276 int res = swprintf_s(buff, sizeof(buff) / sizeof(buff[0]), |
| 277 L"ProcessPolicyTest_%08x", pid); |
| 278 if (-1 != res) { |
| 279 return std::wstring(buff); |
| 280 } |
| 281 return std::wstring(); |
| 282 } |
| 283 |
| 284 // This is the function that is called when testing thread creation. |
| 285 // It is expected to set an event that the caller is waiting on. |
| 286 DWORD TestThreadFunc(LPVOID lpdwThreadParam) { |
| 287 std::wstring event_name = GenerateEventName((DWORD)lpdwThreadParam); |
| 288 if (!event_name.length()) { |
| 289 return 1; |
| 290 } |
| 291 HANDLE event = ::OpenEvent(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, FALSE, |
| 292 event_name.c_str()); |
| 293 if (!event) { |
| 294 return 1; |
| 295 } |
| 296 if (!SetEvent(event)) { |
| 297 return 1; |
| 298 } |
| 299 return 0; |
| 300 } |
| 301 |
| 302 SBOX_TESTS_COMMAND int Process_CreateThread(int argc, wchar_t** argv) { |
| 303 DWORD pid = ::GetCurrentProcessId(); |
| 304 std::wstring event_name = GenerateEventName(pid); |
| 305 if (!event_name.length()) { |
| 306 return SBOX_TEST_FIRST_ERROR; |
| 307 } |
| 308 HANDLE event = ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); |
| 309 if (!event) { |
| 310 return SBOX_TEST_SECOND_ERROR; |
| 311 } |
| 312 |
| 313 DWORD thread_id = 0; |
| 314 HANDLE thread = NULL; |
| 315 thread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&TestThreadFunc, |
| 316 (LPVOID)pid, 0, &thread_id); |
| 317 |
| 318 if (!thread) { |
| 319 return SBOX_TEST_THIRD_ERROR; |
| 320 } |
| 321 if (!thread_id) { |
| 322 return SBOX_TEST_FOURTH_ERROR; |
| 323 } |
| 324 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) { |
| 325 return SBOX_TEST_FIFTH_ERROR; |
| 326 } |
| 327 DWORD exit_code = 0; |
| 328 if (!GetExitCodeThread(thread, &exit_code)) { |
| 329 return SBOX_TEST_SIXTH_ERROR; |
| 330 } |
| 331 if (exit_code) { |
| 332 return SBOX_TEST_SEVENTH_ERROR; |
| 333 } |
| 334 if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) { |
| 335 return SBOX_TEST_FAILED; |
| 336 } |
| 337 return SBOX_TEST_SUCCEEDED; |
| 338 } |
| 339 |
272 TEST(ProcessPolicyTest, TestAllAccess) { | 340 TEST(ProcessPolicyTest, TestAllAccess) { |
273 // Check if the "all access" rule fails to be added when the token is too | 341 // Check if the "all access" rule fails to be added when the token is too |
274 // powerful. | 342 // powerful. |
275 TestRunner runner; | 343 TestRunner runner; |
276 | 344 |
277 // Check the failing case. | 345 // Check the failing case. |
278 runner.GetPolicy()->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); | 346 runner.GetPolicy()->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); |
279 EXPECT_EQ(SBOX_ERROR_UNSUPPORTED, | 347 EXPECT_EQ(SBOX_ERROR_UNSUPPORTED, |
280 runner.GetPolicy()->AddRule(TargetPolicy::SUBSYS_PROCESS, | 348 runner.GetPolicy()->AddRule(TargetPolicy::SUBSYS_PROCESS, |
281 TargetPolicy::PROCESS_ALL_EXEC, | 349 TargetPolicy::PROCESS_ALL_EXEC, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 458 |
391 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | 459 EXPECT_EQ(SBOX_TEST_SUCCEEDED, |
392 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); | 460 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); |
393 } | 461 } |
394 | 462 |
395 TEST(ProcessPolicyTest, TestCreateProcessA) { | 463 TEST(ProcessPolicyTest, TestCreateProcessA) { |
396 TestRunner runner; | 464 TestRunner runner; |
397 sandbox::TargetPolicy* policy = runner.GetPolicy(); | 465 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
398 policy->SetJobLevel(JOB_NONE, 0); | 466 policy->SetJobLevel(JOB_NONE, 0); |
399 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); | 467 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); |
400 | |
401 base::string16 exe_path = MakePathToSys(L"calc.exe", false); | 468 base::string16 exe_path = MakePathToSys(L"calc.exe", false); |
402 ASSERT_TRUE(!exe_path.empty()); | 469 ASSERT_TRUE(!exe_path.empty()); |
403 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, | 470 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
404 TargetPolicy::PROCESS_ALL_EXEC, exe_path.c_str())); | 471 TargetPolicy::PROCESS_ALL_EXEC, exe_path.c_str())); |
405 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | 472 EXPECT_EQ(SBOX_TEST_SUCCEEDED, |
406 runner.RunTest(L"Process_CreateProcessA calc.exe")); | 473 runner.RunTest(L"Process_CreateProcessA calc.exe")); |
407 } | 474 } |
408 | 475 |
409 } // namespace sandbox | 476 // This tests that the CreateThread works with CSRSS not locked down. |
| 477 // In other words, that the interception passes through OK. |
| 478 TEST(ProcessPolicyTest, TestCreateThreadWithCsrss) { |
| 479 TestRunner runner(JOB_NONE, USER_INTERACTIVE, USER_INTERACTIVE); |
| 480 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_CreateThread")); |
| 481 } |
| 482 |
| 483 // This tests that the CreateThread works with CSRSS locked down. |
| 484 // In other words, that the interception correctly works. |
| 485 TEST(ProcessPolicyTest, TestCreateThreadWithoutCsrss) { |
| 486 TestRunner runner(JOB_NONE, USER_INTERACTIVE, USER_INTERACTIVE); |
| 487 |
| 488 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
| 489 TargetPolicy::PROCESS_ALL_EXEC, |
| 490 L"this is not important")); |
| 491 |
| 492 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_CreateThread")); |
| 493 } |
| 494 |
| 495 // This tests that our CreateThread interceptors works when called directly. |
| 496 TEST(ProcessPolicyTest, TestCreateThreadOutsideSandbox) { |
| 497 DWORD pid = ::GetCurrentProcessId(); |
| 498 std::wstring event_name = GenerateEventName(pid); |
| 499 ASSERT_STRNE(NULL, event_name.c_str()); |
| 500 HANDLE event = ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); |
| 501 EXPECT_NE(NULL, int(event)); |
| 502 |
| 503 DWORD thread_id = 0; |
| 504 HANDLE thread = NULL; |
| 505 thread = TargetCreateThread(::CreateThread, NULL, 0, |
| 506 (LPTHREAD_START_ROUTINE)&TestThreadFunc, |
| 507 (LPVOID)pid, 0, &thread_id); |
| 508 EXPECT_NE(NULL, int(thread)); |
| 509 EXPECT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE)); |
| 510 EXPECT_EQ(WAIT_OBJECT_0, WaitForSingleObject(event, INFINITE)); |
| 511 } |
| 512 |
| 513 } // namespace sandbox |
OLD | NEW |