| OLD | NEW |
| 1 // Copyright (c) 2011 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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/process/launch.h" | 9 #include "base/process/launch.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 197 |
| 198 EXPECT_EQ((is_success_test ? sandbox::SBOX_TEST_SUCCEEDED | 198 EXPECT_EQ((is_success_test ? sandbox::SBOX_TEST_SUCCEEDED |
| 199 : sandbox::SBOX_TEST_FAILED), | 199 : sandbox::SBOX_TEST_FAILED), |
| 200 runner.RunTest(test.c_str())); | 200 runner.RunTest(test.c_str())); |
| 201 } | 201 } |
| 202 | 202 |
| 203 } // namespace | 203 } // namespace |
| 204 | 204 |
| 205 namespace sandbox { | 205 namespace sandbox { |
| 206 | 206 |
| 207 // A shared helper test command that will attempt to CreateProcess | 207 // A shared helper test command that will attempt to CreateProcess with a given |
| 208 // with a given command line. The second parameter, if set to non-zero | 208 // command line. The second optional parameter will cause the child process to |
| 209 // will cause the child process to return exit code STATUS_ACCESS_VIOLATION. | 209 // return that as an exit code on termination. |
| 210 // | 210 // |
| 211 // ***Make sure you've enabled basic process creation in the | 211 // ***Make sure you've enabled basic process creation in the |
| 212 // test sandbox settings via: | 212 // test sandbox settings via: |
| 213 // sandbox::TargetPolicy::SetJobLevel(), | 213 // sandbox::TargetPolicy::SetJobLevel(), |
| 214 // sandbox::TargetPolicy::SetTokenLevel(), | 214 // sandbox::TargetPolicy::SetTokenLevel(), |
| 215 // and TestRunner::SetDisableCsrss(). | 215 // and TestRunner::SetDisableCsrss(). |
| 216 SBOX_TESTS_COMMAND int TestChildProcess(int argc, wchar_t** argv) { | 216 SBOX_TESTS_COMMAND int TestChildProcess(int argc, wchar_t** argv) { |
| 217 if (argc < 2) | 217 if (argc < 1) |
| 218 return SBOX_TEST_INVALID_PARAMETER; | 218 return SBOX_TEST_INVALID_PARAMETER; |
| 219 | 219 |
| 220 int desired_exit_code = _wtoi(argv[1]); | 220 int desired_exit_code = 0; |
| 221 if (desired_exit_code) | 221 |
| 222 desired_exit_code = STATUS_ACCESS_VIOLATION; | 222 if (argc == 2) { |
| 223 desired_exit_code = wcstoul(argv[1], nullptr, 0); |
| 224 } |
| 223 | 225 |
| 224 std::wstring cmd = argv[0]; | 226 std::wstring cmd = argv[0]; |
| 225 base::LaunchOptions options = base::LaunchOptionsForTest(); | 227 base::LaunchOptions options = base::LaunchOptionsForTest(); |
| 226 base::Process setup_proc = base::LaunchProcess(cmd.c_str(), options); | 228 base::Process setup_proc = base::LaunchProcess(cmd.c_str(), options); |
| 227 | 229 |
| 228 if (setup_proc.IsValid()) { | 230 if (setup_proc.IsValid()) { |
| 229 setup_proc.Terminate(desired_exit_code, false); | 231 setup_proc.Terminate(desired_exit_code, false); |
| 230 return SBOX_TEST_SUCCEEDED; | 232 return SBOX_TEST_SUCCEEDED; |
| 231 } | 233 } |
| 232 // Note: GetLastError from CreateProcess returns 5, "ERROR_ACCESS_DENIED". | 234 // Note: GetLastError from CreateProcess returns 5, "ERROR_ACCESS_DENIED". |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 policy->SetJobLevel(JOB_INTERACTIVE, 0); | 672 policy->SetJobLevel(JOB_INTERACTIVE, 0); |
| 671 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); | 673 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); |
| 672 runner.SetDisableCsrss(false); | 674 runner.SetDisableCsrss(false); |
| 673 | 675 |
| 674 base::FilePath cmd; | 676 base::FilePath cmd; |
| 675 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); | 677 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); |
| 676 cmd = cmd.Append(L"calc.exe"); | 678 cmd = cmd.Append(L"calc.exe"); |
| 677 | 679 |
| 678 std::wstring test_command = L"TestChildProcess "; | 680 std::wstring test_command = L"TestChildProcess "; |
| 679 test_command += cmd.value().c_str(); | 681 test_command += cmd.value().c_str(); |
| 680 test_command += L" 0"; | |
| 681 | 682 |
| 682 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str())); | 683 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str())); |
| 683 } | 684 } |
| 684 | 685 |
| 685 // This test validates that setting the | 686 // This test validates that setting the |
| 686 // MITIGATION_CHILD_PROCESS_CREATION_RESTRICTED mitigation prevents | 687 // MITIGATION_CHILD_PROCESS_CREATION_RESTRICTED mitigation prevents |
| 687 // the spawning of child processes. | 688 // the spawning of child processes. |
| 688 TEST(ProcessMitigationsTest, CheckChildProcessFailure) { | 689 TEST(ProcessMitigationsTest, CheckChildProcessFailure) { |
| 689 TestRunner runner; | 690 TestRunner runner; |
| 690 sandbox::TargetPolicy* policy = runner.GetPolicy(); | 691 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 691 | 692 |
| 692 // Now set the job level to be <= JOB_LIMITED_USER | 693 // Now set the job level to be <= JOB_LIMITED_USER |
| 693 // and ensure we can no longer create a child process. | 694 // and ensure we can no longer create a child process. |
| 694 policy->SetJobLevel(JOB_LIMITED_USER, 0); | 695 policy->SetJobLevel(JOB_LIMITED_USER, 0); |
| 695 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); | 696 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); |
| 696 runner.SetDisableCsrss(false); | 697 runner.SetDisableCsrss(false); |
| 697 | 698 |
| 698 base::FilePath cmd; | 699 base::FilePath cmd; |
| 699 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); | 700 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); |
| 700 cmd = cmd.Append(L"calc.exe"); | 701 cmd = cmd.Append(L"calc.exe"); |
| 701 | 702 |
| 702 std::wstring test_command = L"TestChildProcess "; | 703 std::wstring test_command = L"TestChildProcess "; |
| 703 test_command += cmd.value().c_str(); | 704 test_command += cmd.value().c_str(); |
| 704 test_command += L" 0"; | |
| 705 | 705 |
| 706 EXPECT_EQ(SBOX_TEST_FAILED, runner.RunTest(test_command.c_str())); | 706 EXPECT_EQ(SBOX_TEST_FAILED, runner.RunTest(test_command.c_str())); |
| 707 } | 707 } |
| 708 | 708 |
| 709 // This test validates that we can spawn a child process if | 709 // This test validates that when the sandboxed target within a job spawns a |
| 710 // MITIGATION_CHILD_PROCESS_CREATION_RESTRICTED mitigation is | 710 // child process and the target process exits abnormally, the broker correctly |
| 711 // not set. This also tests that a crashing child process is correctly handled | 711 // handles the JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS message. |
| 712 // by the broker. | 712 // Because this involves spawning a child process from the target process and is |
| 713 TEST(ProcessMitigationsTest, CheckChildProcessSuccessAbnormalExit) { | 713 // very similar to the above CheckChildProcess* tests, this test is here rather |
| 714 // than elsewhere closer to the other Job tests. |
| 715 TEST(ProcessMitigationsTest, CheckChildProcessAbnormalExit) { |
| 714 TestRunner runner; | 716 TestRunner runner; |
| 715 sandbox::TargetPolicy* policy = runner.GetPolicy(); | 717 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 716 | 718 |
| 717 // Set a policy that would normally allow for process creation. | 719 // Set a policy that would normally allow for process creation. |
| 718 policy->SetJobLevel(JOB_INTERACTIVE, 0); | 720 policy->SetJobLevel(JOB_INTERACTIVE, 0); |
| 719 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); | 721 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); |
| 720 runner.SetDisableCsrss(false); | 722 runner.SetDisableCsrss(false); |
| 721 | 723 |
| 722 base::FilePath cmd; | 724 base::FilePath cmd; |
| 723 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); | 725 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); |
| 724 cmd = cmd.Append(L"calc.exe"); | 726 cmd = cmd.Append(L"calc.exe"); |
| 725 | 727 |
| 726 std::wstring test_command = L"TestChildProcess "; | 728 std::wstring test_command(base::StringPrintf(L"TestChildProcess %ls 0x%08X", |
| 727 test_command += cmd.value().c_str(); | 729 cmd.value().c_str(), |
| 728 test_command += L" 1"; | 730 STATUS_ACCESS_VIOLATION)); |
| 729 | 731 |
| 730 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str())); | 732 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str())); |
| 731 } | 733 } |
| 732 | 734 |
| 733 // This test validates that setting the | |
| 734 // MITIGATION_CHILD_PROCESS_CREATION_RESTRICTED mitigation prevents | |
| 735 // the spawning of child processes. This also tests that a crashing child | |
| 736 // process is correctly handled by the broker. | |
| 737 TEST(ProcessMitigationsTest, CheckChildProcessFailureAbnormalExit) { | |
| 738 TestRunner runner; | |
| 739 sandbox::TargetPolicy* policy = runner.GetPolicy(); | |
| 740 | |
| 741 // Now set the job level to be <= JOB_LIMITED_USER | |
| 742 // and ensure we can no longer create a child process. | |
| 743 policy->SetJobLevel(JOB_LIMITED_USER, 0); | |
| 744 policy->SetTokenLevel(USER_UNPROTECTED, USER_UNPROTECTED); | |
| 745 runner.SetDisableCsrss(false); | |
| 746 | |
| 747 base::FilePath cmd; | |
| 748 EXPECT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &cmd)); | |
| 749 cmd = cmd.Append(L"calc.exe"); | |
| 750 | |
| 751 std::wstring test_command = L"TestChildProcess "; | |
| 752 test_command += cmd.value().c_str(); | |
| 753 test_command += L" 1"; | |
| 754 | |
| 755 EXPECT_EQ(SBOX_TEST_FAILED, runner.RunTest(test_command.c_str())); | |
| 756 } | |
| 757 | |
| 758 } // namespace sandbox | 735 } // namespace sandbox |
| OLD | NEW |