Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/stringprintf.h" | |
| 6 #include "sandbox/src/handle_policy.h" | |
| 7 #include "sandbox/src/nt_internals.h" | |
| 8 #include "sandbox/src/sandbox.h" | |
| 9 #include "sandbox/src/sandbox_factory.h" | |
| 10 #include "sandbox/src/sandbox_policy.h" | |
| 11 #include "sandbox/src/win_utils.h" | |
| 12 #include "sandbox/tests/common/controller.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace sandbox { | |
| 16 | |
| 17 // Just waits for the supplied number of milliseconds. | |
| 18 SBOX_TESTS_COMMAND int Handle_WaitProcess(int argc, wchar_t **argv) { | |
| 19 if (argc != 1) | |
| 20 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
| 21 | |
| 22 ::Sleep(::wcstoul(argv[0], NULL, 10)); | |
| 23 return SBOX_TEST_TIMED_OUT; | |
| 24 } | |
| 25 | |
| 26 // Attempts to duplicate an event handle into the target process. | |
| 27 SBOX_TESTS_COMMAND int Handle_DuplicateEvent(int argc, wchar_t **argv) { | |
| 28 if (argc != 1) | |
| 29 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
| 30 | |
| 31 // Create a test event to use as a handle. | |
| 32 base::win::ScopedHandle test_event(::CreateEvent( | |
|
rvargas (doing something else)
2012/03/27 00:35:33
nit: do you mind moving CreateEvent to the next li
jschuh
2012/03/27 01:36:19
Done.
| |
| 33 NULL, TRUE, TRUE, NULL)); | |
| 34 if (!test_event.IsValid()) | |
| 35 return SBOX_TEST_FIRST_ERROR; | |
| 36 | |
| 37 // Get the target process ID. | |
| 38 DWORD target_process_id = ::wcstoul(argv[0], NULL, 10); | |
| 39 | |
| 40 HANDLE handle = NULL; | |
| 41 ResultCode result = SandboxFactory::GetTargetServices()->DuplicateHandle( | |
| 42 test_event, target_process_id, &handle, 0, FALSE, DUPLICATE_SAME_ACCESS); | |
| 43 | |
| 44 return (result == SBOX_ALL_OK) ? SBOX_TEST_SUCCEEDED : SBOX_TEST_DENIED; | |
| 45 } | |
| 46 | |
| 47 // Tests that we duplicating an object works only when the policy allows it. | |
|
rvargas (doing something else)
2012/03/27 00:35:33
nit: drop "we"
jschuh
2012/03/27 01:36:19
Done.
| |
| 48 TEST(HandlePolicyTest, DuplicateHandle) { | |
| 49 TestRunner target; | |
| 50 TestRunner runner; | |
| 51 | |
| 52 // Kick off an asynchronous target process for testing. | |
| 53 target.SetAsynchronous(true); | |
| 54 EXPECT_EQ(SBOX_TEST_SUCCEEDED, target.RunTest(L"Handle_WaitProcess 30000")); | |
| 55 | |
| 56 // First test that we fail to open the event. | |
| 57 std::wstring cmd_line = base::StringPrintf(L"Handle_DuplicateEvent %d", | |
| 58 target.process_id()); | |
| 59 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(cmd_line.c_str())); | |
| 60 | |
| 61 // Now successfully open the event after adding a duplicate handle rule. | |
| 62 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_HANDLES, | |
| 63 TargetPolicy::HANDLES_DUP_ANY, | |
| 64 L"Event")); | |
| 65 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd_line.c_str())); | |
| 66 } | |
| 67 | |
| 68 } // namespace sandbox | |
| 69 | |
| OLD | NEW |